﻿// 自定义
Ext.define('Html5App.custom', {
	alternateClassName : 'custom',
	statics : {
		thousandBitSeparator : function(num,fixed) {
			num = Number(num).toFixed(fixed) + "";
            var re = /(-?\d+)(\d{3})/;
            while (re.test(num)) {
                num = num.replace(re, "$1,$2");
            }
            return num;
		},
		getCurrentMonthFirst : function (){
			var date=new Date();
			date.setDate(1);
			var dateStr = '';
			var curMonth = parseInt(date.getMonth()+1);
		    dateStr += date.getFullYear();
		    dateStr += '';
			dateStr += curMonth < 10 ? '0'+curMonth : curMonth;
			dateStr += '';
			dateStr += date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
			return dateStr;
		},
		getCurrentMonthLast : function (){
			var date=new Date();
 			var currentMonth=date.getMonth();
 			var nextMonth=++currentMonth;
 			var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
 			var oneDay=1000*60*60*24;
 			var date = new Date(nextMonthFirstDay-oneDay);
 			var dateStr = '';
			var curMonth = parseInt(date.getMonth()+1);
		    dateStr += date.getFullYear();
		    dateStr += '';
			dateStr += curMonth < 10 ? '0'+curMonth : curMonth;
			dateStr += '';
			dateStr += date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
			return dateStr;
		},
		getMonthFirst : function (y,m){
			var date=new Date(y,m,1);
			date.setDate(1);
			var dateStr = '';
			var curMonth = parseInt(date.getMonth()+1);
		    dateStr += date.getFullYear();
		    dateStr += '';
			dateStr += curMonth < 10 ? '0'+curMonth : curMonth;
			dateStr += '';
			dateStr += date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
			return dateStr;
		},
		getMonthLast : function (y,m){
			var date=new Date(y,m,1);
 			var currentMonth=date.getMonth();
 			var nextMonth=++currentMonth;
 			var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
 			var oneDay=1000*60*60*24;
 			var date = new Date(nextMonthFirstDay-oneDay);
 			var dateStr = '';
			var curMonth = parseInt(date.getMonth()+1);
		    dateStr += date.getFullYear();
		    dateStr += '';
			dateStr += curMonth < 10 ? '0'+curMonth : curMonth;
			dateStr += '';
			dateStr += date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
			return dateStr;
		},
		orderSettle : function(){
			var OrderNo = Number(Ext.maxOrderNo);
			if(Number(Ext.maxOrderNo) == 1){
				OrderNo = Math.floor(Math.random()*100000+1);
				Ext.maxOrderNo = Number(OrderNo);
			}
			var SalesCode = config.current_user.user_name;
			var OrderDate = Ext.getCmp("order-date").getHtml();
			var DeliveryDate = Ext.getCmp("delivery-date").getHtml();
			var CustomerCode = Ext.getCmp("customer-new-order-customer-name").getData();
			var CustomerName = Ext.getCmp("customer-new-order-customer-name").getHtml().split(" ")[1];
			var Address = Number(Ext.getCmp("delivery-address").getHtml().split(" ")[0]);
			var AddressStr = Ext.getCmp("delivery-address").getHtml();
			var Payment = "3";
			if(Ext.getCmp("payment-mode").getHtml() == "月結"){
				Payment = "1";
			}else if(Ext.getCmp("payment-mode").getHtml() == "現款"){
				Payment = "2";
			}
			var TotalAmount = Ext.getCmp("orderTotal").getHtml().replace("$","").replace(",","");
			var NetAmount = Ext.getCmp("orderTotal").getHtml().replace("$","").replace(",","");
			var now = new Date();
			var curDate = '';
			var curMonth = parseInt(now.getMonth()+1);
		    curDate += now.getFullYear();
		    curDate += '-';
			curDate += curMonth < 10 ? '0'+curMonth : curMonth;
			curDate += '-';
			curDate += now.getDate() < 10 ? '0'+now.getDate() : now.getDate();
			curDate += ' ';
			curDate += now.getHours() < 10 ? '0'+now.getHours() : now.getHours();
			curDate += ':';
			curDate += now.getMinutes() < 10 ? '0'+now.getMinutes() : now.getMinutes();
			curDate += ':';
			curDate += now.getSeconds() < 10 ? '0'+now.getSeconds() : now.getSeconds();
			var CreateTime = curDate;
			var ConvertFlag = "0"
			
			var orderItemTable=document.getElementById('orderItem');
			if(orderItemTable.rows.length > 0){
				var validation = true;
				for(var i=0; i<orderItemTable.rows.length; i++){
					var trObj=orderItemTable.rows[i];
					if(trObj.cells[1].innerHTML == "&nbsp;" || trObj.cells[1].innerHTML == ""){
						util.showMessageMask('第'+Number(Number(i)+1)+'行貨品為空，不能結算','warn-max');
						validation = false;
						return;
					}
				}
				if(validation){
					var TotalQty = 0;
					var DetailCount = 0
					config.database.transaction(
						function (tx) {
							if(config.parameter.OrderNo){
								tx.executeSql("delete from OrderHead where SalesCode='"+config.current_user.user_name+"' and OrderNo="+config.parameter.OrderNo,[],function (tx, result) {},function (tx, error) {});
								tx.executeSql("delete from OrderDetail where SalesCode='"+config.current_user.user_name+"' and OrderNo="+config.parameter.OrderNo,[],function (tx, result) {},function (tx, error) {});
								OrderNo = config.parameter.OrderNo;
							}
							for(var i=0; i<orderItemTable.rows.length; i++){
								var trObj=orderItemTable.rows[i];
								if(trObj.cells[1].innerHTML == "&nbsp;" || trObj.cells[1].innerHTML == ""){
									util.showMessageMask('第'+Number(Number(i)+1)+'行貨品為空，不能結算','warn-max');
									return;
								}
								var DetailNo = Number(Number(i)+1);
								var ItemCode = document.getElementById("itemId"+i).value;
								var ItemName = trObj.cells[1].innerHTML;
								var Unit = trObj.cells[2].innerHTML;
								var OrderQty = document.getElementById("itemNum"+i).value;
								var ICOQty = "0";
								var StockQty = "0";
								var Price = trObj.cells[4].innerHTML.replace("$","").replace(",","");
								var SubTotal = trObj.cells[5].innerHTML.replace("$","").replace(",","");
								TotalQty = Number(Number(TotalQty) + Number(document.getElementById("itemNum"+i).value));
								DetailCount = Number(Number(i)+1);
							
				            	tx.executeSql("insert into OrderDetail(OrderNo,DetailNo,SalesCode,ItemCode,ItemName,Unit,ICOQty,StockQty,OrderQty,Price,SubTotal,isSend)" +
				                				" values ("+OrderNo+","+DetailNo+",'"+SalesCode+"','"+ItemCode+"','"+ItemName+"','"+Unit+"','"+ICOQty+"','"+StockQty+"','"+OrderQty+"','"+Price+"','"+SubTotal+"',0)",[],function (tx, result) {},function (tx, error) {});
				            }
			            	tx.executeSql("insert into OrderHead(OrderNo,SalesCode,OrderDate,DeliveryDate,CustomerCode,CustomerName,Address,AddressStr,Payment,TotalAmount,NetAmount,TotalQty,DetailCount,CreateTime,UpdateTime,ConvertFlag,isSend)" +
			                				" values ("+OrderNo+",'"+SalesCode+"','"+OrderDate+"','"+DeliveryDate+"','"+CustomerCode+"','"+CustomerName+"',"+Address+",'"+AddressStr+"',"+Payment+",'"+TotalAmount+"','"+NetAmount+"','"+TotalQty+"',"+DetailCount+",'"+CreateTime+"','"+CreateTime+"',"+ConvertFlag+",0)",[],function (tx, result) {},function (tx, error) {});
						}
					);
					Ext.maxOrderNo = Number(Number(Ext.maxOrderNo)+1);
					config.order_item_arr = [];
					Ext.getCmp("titleBar").setActiveItem(2);
					Ext.getCmp("orderHome").removeAll();
					Ext.getCmp("orderHome").add({
						xtype : 'orderList'
					});
				}
			}else{
				util.showMessageMask('請先選擇貨品','warn-max');
			}
		},
		// 下载数据以及产品图片
		downloadDataBySalesCode : function(SalesCodeStr,isShowHome) {
			util.showLoadMaskMax('正在下載數據...');
			var tableNameArr = new Array();
			tableNameArr[0] = '下載SalesMan數據...';
			tableNameArr[1] = '下載Customer數據...';
			tableNameArr[2] = '下載Area數據...';
			tableNameArr[3] = '下載TAllSalesDataPK數據...';
			tableNameArr[4] = '下載CustomerBrandTarget數據...';
			tableNameArr[5] = '下載Brand數據...';
			tableNameArr[6] = '下載Item數據...';
			tableNameArr[7] = '下載CustomerAddress數據...';
			tableNameArr[8] = '下載ItemShort數據...';
			tableNameArr[9] = '下載ItemChinese數據...';
			tableNameArr[10] = '下載SalesAllotment數據...';
			tableNameArr[11] = '下載SalesAllotment數據...';
			tableNameArr[12] = '下載SalesBrandTarget數據...';
			tableNameArr[13] = '下載FOrderHead數據...';
			tableNameArr[14] = '下載FOrderDetail數據...';
			tableNameArr[15] = '處理Golden45Sales數據...';
			var indexNum = 0;
			Ext.timerObj = setInterval(function showLoadMaskFun(){
				if(indexNum == 16){
					clearInterval(Ext.timerObj);
				} else {
					util.showLoadMaskMax((indexNum+1)+'/16' +' '+tableNameArr[indexNum]);
					indexNum++;
				}
			},4500);
			Ext.data.JsonP.request({
                url: config.global_url+'/downloadData.html',
                callbackKey: 'callback',
                params: {
                	SalesCode :  SalesCodeStr
                },
                timeout: 600000,
                success: function(result, request) {
                	if (result.result) {
                		config.database.transaction(
							function (tx) {
								
								//清空SalesMan表
								tx.executeSql("delete from SalesMan",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.SalesManList && result.SalesManList.length>0){
                        			for(var i=0;i<result.SalesManList.length;i++){
                        				//写入SalesMan表
                        				tx.executeSql("insert into SalesMan(SalesCode,Passwd,SalesName) values ('"+result.SalesManList[i].SalesCode+"','"+result.SalesManList[i].Passwd+"','"+result.SalesManList[i].SalesName+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空Customer表
								tx.executeSql("delete from Customer",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.CustomerList && result.CustomerList.length>0){
                        			for(var i=0;i<result.CustomerList.length;i++){
                        				//写入Customer表
                        				tx.executeSql("insert into Customer(CustomerCode,SalesCode,CustomerName,DeliveryAddress,Payment,Address1,Address2,Address3,Phone,Fax,CodeTerr,ChannelID,NameCtac,CodeTerm,TextDesc,AMTCRLIMT,AllInvAmt,Balance,T1,A1,Note) " +
                        							  "values ('"+result.CustomerList[i].CustomerCode+"','"+result.CustomerList[i].SalesCode+"','"+result.CustomerList[i].CustomerName+"'," +
                        							  		  "'"+result.CustomerList[i].DeliveryAddress+"','"+result.CustomerList[i].Payment+"','"+result.CustomerList[i].Address1+"'," +
                        							  		  "'"+result.CustomerList[i].Address2+"','"+result.CustomerList[i].Address3+"','"+result.CustomerList[i].Phone+"'," +
                        							  		  "'"+result.CustomerList[i].Fax+"','"+result.CustomerList[i].CodeTerr+"','"+result.CustomerList[i].ChannelID+"'," +
                        							  		  "'"+result.CustomerList[i].NameCtac+"','"+result.CustomerList[i].CodeTerm+"','"+result.CustomerList[i].TextDesc+"'," +
                        							  		  "'"+result.CustomerList[i].AMTCRLIMT+"','"+result.CustomerList[i].AllInvAmt+"','"+result.CustomerList[i].Balance+"'," +
                        							  		  "'"+result.CustomerList[i].T1+"','"+result.CustomerList[i].A1+"','"+result.CustomerList[i].Note+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空Area表
								tx.executeSql("delete from Area",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.AreaList && result.AreaList.length>0){
                        			for(var i=0;i<result.AreaList.length;i++){
                        				//写入Area表
                        				tx.executeSql("insert into Area(SalesCode,CodeTerr,NameTerr) " +
                        							  "values ('"+config.current_user.user_name+"','"+result.AreaList[i].CodeTerr+"','"+result.AreaList[i].NameTerr+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空TAllSalesDataPK表
								tx.executeSql("delete from TAllSalesDataPK",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.TAllSalesDataPKList && result.TAllSalesDataPKList.length>0){
                        			for(var i=0;i<result.TAllSalesDataPKList.length;i++){
                        				//写入TAllSalesDataPK表
                        				tx.executeSql("insert into TAllSalesDataPK(ORDUNIQ,LINENUM,ORDNUMBER,CUSTOMER,BILNAME,ORDDATE,SALESPER1,ITEM,DESC,ACCTSET,CNTLACCT,OPTFLD4,OPTFLD5,ORIGQTY,QTYSHIPPED,ORDUNIT,UNITPRICE,QTYSHPTODT,QTYBACKORD,SRCode,OCTN,OPC,SCTN,SPC,OrderAmt,ShipAmt,OrderSU,ShipSU,PICKSEQ,DistrictCode,DistrictName,OrdType,Category,ShipTo,ShipAddress,QTYORDERED) " +
                        							  "values ('"+result.TAllSalesDataPKList[i].ORDUNIQ+"','"+result.TAllSalesDataPKList[i].LINENUM+"','"+result.TAllSalesDataPKList[i].ORDNUMBER+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].CUSTOMER+"','"+result.TAllSalesDataPKList[i].BILNAME+"','"+result.TAllSalesDataPKList[i].ORDDATE+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].SALESPER1+"','"+result.TAllSalesDataPKList[i].ITEM+"','"+result.TAllSalesDataPKList[i].DESC+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].ACCTSET+"','"+result.TAllSalesDataPKList[i].CNTLACCT+"','"+result.TAllSalesDataPKList[i].OPTFLD4+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].OPTFLD5+"','"+result.TAllSalesDataPKList[i].ORIGQTY+"','"+result.TAllSalesDataPKList[i].QTYSHIPPED+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].ORDUNIT+"','"+result.TAllSalesDataPKList[i].UNITPRICE+"','"+result.TAllSalesDataPKList[i].QTYSHPTODT+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].QTYBACKORD+"','"+result.TAllSalesDataPKList[i].SRCode+"','"+result.TAllSalesDataPKList[i].OCTN+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].OPC+"','"+result.TAllSalesDataPKList[i].SCTN+"','"+result.TAllSalesDataPKList[i].SPC+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].OrderAmt+"','"+result.TAllSalesDataPKList[i].ShipAmt+"','"+result.TAllSalesDataPKList[i].OrderSU+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].ShipSU+"','"+result.TAllSalesDataPKList[i].PICKSEQ+"','"+result.TAllSalesDataPKList[i].DistrictCode+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].DistrictName+"','"+result.TAllSalesDataPKList[i].OrdType+"','"+result.TAllSalesDataPKList[i].Category+"'," +
                        							  		  "'"+result.TAllSalesDataPKList[i].ShipTo+"','"+result.TAllSalesDataPKList[i].ShipAddress+"','"+result.TAllSalesDataPKList[i].QTYORDERED+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空CustomerBrandTarget表
								tx.executeSql("delete from CustomerBrandTarget",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.CustomerBrandTargetList && result.CustomerBrandTargetList.length>0){
                        			for(var i=0;i<result.CustomerBrandTargetList.length;i++){
                        				//写入CustomerBrandTarget表
                        				tx.executeSql("insert into CustomerBrandTarget(CustomerCode,BrandCode,SalesCode,Target,Actual,IndexStr) " +
                        							  "values ('"+result.CustomerBrandTargetList[i].CustomerCode+"','"+result.CustomerBrandTargetList[i].BrandCode+"','"+result.CustomerBrandTargetList[i].SalesCode+"'," +
                        							  		  "'"+result.CustomerBrandTargetList[i].Target+"','"+result.CustomerBrandTargetList[i].Actual+"','"+result.CustomerBrandTargetList[i].Index+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空Brand表
								tx.executeSql("delete from Brand",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.BrandList && result.BrandList.length>0){
                        			for(var i=0;i<result.BrandList.length;i++){
                        				//写入Brand表
                        				tx.executeSql("insert into Brand(BrandCode,SalesCode,BrandName,CategoryCode) " +
                        							  "values ('"+result.BrandList[i].BrandCode+"','"+result.BrandList[i].SalesCode+"','"+result.BrandList[i].BrandName+"'," +
                        							  		  "'"+result.BrandList[i].CategoryCode+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空Item表
								tx.executeSql("delete from Item",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.ItemList && result.ItemList.length>0){
                        			for(var i=0;i<result.ItemList.length;i++){
                        				//写入Item表
                        				tx.executeSql("insert into Item(ItemCode,SalesCode,ItemName,BrandID,Price,OPTFLD4,optfld7,optfld8,optfld9,optfld10,optfld11,Unit,optfld3,OPTAMT,OPTFLD5,UNITWGT) " +
                        							  "values ('"+result.ItemList[i].ItemCode+"','"+result.ItemList[i].SalesCode+"','"+result.ItemList[i].ItemName+"'," +
                        							  		  "'"+result.ItemList[i].BrandID+"','"+result.ItemList[i].Price+"','"+result.ItemList[i].OPTFLD4+"'," +
                        							  		  "'"+result.ItemList[i].optfld7+"','"+result.ItemList[i].optfld8+"','"+result.ItemList[i].optfld9+"'," +
                        							  		  "'"+result.ItemList[i].optfld10+"','"+result.ItemList[i].optfld11+"','"+result.ItemList[i].Unit+"'," +
                        							  		  "'"+result.ItemList[i].optfld3+"','"+result.ItemList[i].OPTAMT+"','"+result.ItemList[i].OPTFLD5+"'," +
                        							  		  "'"+result.ItemList[i].UNITWGT+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空CustomerAddress表
								tx.executeSql("delete from CustomerAddress",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.CustomerAddressList && result.CustomerAddressList.length>0){
                        			for(var i=0;i<result.CustomerAddressList.length;i++){
                        				//写入CustomerAddress表
                        				tx.executeSql("insert into CustomerAddress(IDCUST,IDCUSTSHPT,SalesCode,NAMELOCN,TEXTSTRE1,TEXTSTRE2,TEXTSTRE3,TEXTSTRE4) " +
                        							  "values ('"+result.CustomerAddressList[i].IDCUST+"','"+result.CustomerAddressList[i].IDCUSTSHPT+"','"+result.CustomerAddressList[i].SalesCode+"'," +
                        							  		  "'"+result.CustomerAddressList[i].NAMELOCN+"','"+result.CustomerAddressList[i].TEXTSTRE1+"','"+result.CustomerAddressList[i].TEXTSTRE2+"'," +
                        							  		  "'"+result.CustomerAddressList[i].TEXTSTRE3+"','"+result.CustomerAddressList[i].TEXTSTRE4+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空ItemShort表
								tx.executeSql("delete from ItemShort",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.ItemShortList && result.ItemShortList.length>0){
                        			for(var i=0;i<result.ItemShortList.length;i++){
                        				//写入ItemShort表
                        				tx.executeSql("insert into ItemShort(ItemCode,ItemName,Brand,Category,ItemShort,UpdateTime) " +
                        							  "values ('"+result.ItemShortList[i].ItemCode+"','"+result.ItemShortList[i].ItemName+"','"+result.ItemShortList[i].Brand+"'," +
                        							  		  "'"+result.ItemShortList[i].Category+"','"+result.ItemShortList[i].ItemShort+"','"+result.ItemShortList[i].UpdateTime+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空ItemChinese表
								tx.executeSql("delete from ItemChinese",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.ItemChineseList && result.ItemChineseList.length>0){
                        			for(var i=0;i<result.ItemChineseList.length;i++){
                        				//写入ItemChinese表
                        				tx.executeSql("insert into ItemChinese(ItemCode,ItemName,PICKINGSEQ) " +
                        							  "values ('"+result.ItemChineseList[i].ItemCode+"','"+result.ItemChineseList[i].ItemName+"','"+result.ItemChineseList[i].PICKINGSEQ+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空SalesAllotment表
								tx.executeSql("delete from SalesAllotment",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.SalesAllotmentList && result.SalesAllotmentList.length>0){
                        			for(var i=0;i<result.SalesAllotmentList.length;i++){
                        				//写入SalesAllotment表
                        				tx.executeSql("insert into SalesAllotment(SALESCODE,ITEMCODE,DATE,RESETDATE,DESCRIPTION,UNIT,QUANTITY) " +
                        							  "values ('"+result.SalesAllotmentList[i].SALESCODE+"','"+result.SalesAllotmentList[i].ITEMCODE+"','"+result.SalesAllotmentList[i].DATE+"'," +
                        							  		  "'"+result.SalesAllotmentList[i].RESETDATE+"','"+result.SalesAllotmentList[i].DESCRIPTION+"','"+result.SalesAllotmentList[i].UNIT+"'," +
                        							  		  "'"+result.SalesAllotmentList[i].QUANTITY+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空SalesAllotment表
								tx.executeSql("delete from SalesAllotment",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.SalesAllotmentList && result.SalesAllotmentList.length>0){
                        			for(var i=0;i<result.SalesAllotmentList.length;i++){
                        				//写入SalesAllotment表
                        				tx.executeSql("insert into SalesAllotment(SALESCODE,ITEMCODE,DATE,RESETDATE,DESCRIPTION,UNIT,QUANTITY) " +
                        							  "values ('"+result.SalesAllotmentList[i].SALESCODE+"','"+result.SalesAllotmentList[i].ITEMCODE+"','"+result.SalesAllotmentList[i].DATE+"'," +
                        							  		  "'"+result.SalesAllotmentList[i].RESETDATE+"','"+result.SalesAllotmentList[i].DESCRIPTION+"','"+result.SalesAllotmentList[i].UNIT+"'," +
                        							  		  "'"+result.SalesAllotmentList[i].QUANTITY+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空SalesBrandTarget表
								tx.executeSql("delete from SalesBrandTarget",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.SalesBrandTargetList && result.SalesBrandTargetList.length>0){
                        			for(var i=0;i<result.SalesBrandTargetList.length;i++){
                        				//写入SalesBrandTarget表
                        				tx.executeSql("insert into SalesBrandTarget(BrandCode,SalesCode,Target,Actual,IndexStr) " +
                        							  "values ('"+result.SalesBrandTargetList[i].BrandCode+"','"+result.SalesBrandTargetList[i].SalesCode+"','"+result.SalesBrandTargetList[i].Target+"'," +
                        							  		  "'"+result.SalesBrandTargetList[i].Actual+"','"+result.SalesBrandTargetList[i].Index+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空FOrderHead表
								tx.executeSql("delete from FOrderHead",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.FOrderHeadList && result.FOrderHeadList.length>0){
                        			for(var i=0;i<result.FOrderHeadList.length;i++){
                        				//写入FOrderHead表
                        				tx.executeSql("insert into FOrderHead(OrderNo,SalesCode,OrderDate,DeliveryDate,CustomerCode,Address,Payment,TotalAmount,NetAmount,TotalQty,DetailCount,CreateTime,UpdateTime,ConvertFlag,OrgOrderNo,FaxTag,BatchNo) " +
                        							  "values ('"+result.FOrderHeadList[i].OrderNo+"','"+result.FOrderHeadList[i].SalesCode+"','"+result.FOrderHeadList[i].OrderDate+"'," +
                        							  		  "'"+result.FOrderHeadList[i].DeliveryDate+"','"+result.FOrderHeadList[i].CustomerCode+"','"+result.FOrderHeadList[i].Address+"'," +
                        							  		  "'"+result.FOrderHeadList[i].Payment+"','"+result.FOrderHeadList[i].TotalAmount+"','"+result.FOrderHeadList[i].NetAmount+"'," +
                        							  		  "'"+result.FOrderHeadList[i].TotalQty+"','"+result.FOrderHeadList[i].DetailCount+"','"+result.FOrderHeadList[i].CreateTime+"'," +
                        							  		  "'"+result.FOrderHeadList[i].UpdateTime+"','"+result.FOrderHeadList[i].ConvertFlag+"','"+result.FOrderHeadList[i].OrgOrderNo+"'," +
                        							  		  "'"+result.FOrderHeadList[i].FaxTag+"','"+result.FOrderHeadList[i].BatchNo+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空FOrderDetail表
								tx.executeSql("delete from FOrderDetail",
									[],
									function (tx, r) {},
									function (tx, e) {}
								);
								if(result.FOrderDetailList && result.FOrderDetailList.length>0){
                        			for(var i=0;i<result.FOrderDetailList.length;i++){
                        				//写入FOrderDetail表
                        				tx.executeSql("insert into FOrderDetail(OrderNo,DetailNo,SalesCode,ItemCode,ICOQty,StockQty,OrderQty,Price,SubTotal,OrgOrderNo) " +
                        							  "values ('"+result.FOrderDetailList[i].OrderNo+"','"+result.FOrderDetailList[i].DetailNo+"','"+result.FOrderDetailList[i].SalesCode+"'," +
                        							  		  "'"+result.FOrderDetailList[i].ItemCode+"','"+result.FOrderDetailList[i].ICOQty+"','"+result.FOrderDetailList[i].StockQty+"'," +
                        							  		  "'"+result.FOrderDetailList[i].OrderQty+"','"+result.FOrderDetailList[i].Price+"','"+result.FOrderDetailList[i].SubTotal+"'," +
                        							  		  "'"+result.FOrderDetailList[i].OrgOrderNo+"')",
											[],
											function (tx, r) {},
											function (tx, e) {}
										);
                        			}
                        		}
								
                        		//清空Golden45Sales
				                tx.executeSql("delete from Golden45Sales",
				                	[],
				                	function (tx, result) {},
				                	function (tx, error) {}
				                );
				                if(result.golden45SalesList && Number(result.golden45SalesList.length) > 0){
				                	for(var i=0;i<result.golden45SalesList.length;i++){
				                		var golden45Sales = result.golden45SalesList[i];
				                		tx.executeSql("insert into Golden45Sales(ntype,SalesCode,AreaCode,AreaName,CustomerCode,CustomerName,IsGolden45,Golden45s,Orders,Other1,Other2,Other3)" +
				                				" values ("+golden45Sales.ntype+",'"+golden45Sales.SalesCode+"','"+golden45Sales.CodeTerr+"','"+golden45Sales.NameTerr+"','"+golden45Sales.CustomerCode+"','"+golden45Sales.CustomerName+"','"+
				                				golden45Sales.IsGolden45+"',"+golden45Sales.Golden45s+","+golden45Sales.Orders+",'"+golden45Sales.Other1+"','"+golden45Sales.Other2+"',0)",
			                				[],
			                				function (tx, result) {},
			                				function (tx, error) {}
			                			);
				                	}
				                }
				                if(Number(result.tempCustomerList.length) > 0){
				                	for(var i=0;i<result.tempCustomerList.length;i++){
				                		var tempCustomer = result.tempCustomerList[i];
				                		tx.executeSql("update Golden45Sales set Orders="+tempCustomer.Orders+" where CustomerCode="+tempCustomer.Customer,
				                			[],
				                			function (tx, result) {},
				                			function (tx, error) {}
				                		);
				                	}
				                }
				                if(Number(result.customerSpecialList.length) > 0){
				                	for(var i=0;i<result.customerSpecialList.length;i++){
				                		var customerSpecial = result.customerSpecialList[i];
				                		tx.executeSql("update Golden45Sales set IsGolden45=1 where CustomerCode="+customerSpecial.CustomerCode,
					                		[],
					                		function (tx, result) {},
					                		function (tx, error) {}
				                		);
				                	}
				                }
				                if(Number(result.distributeSpecialList.length) > 0){
				                	for(var i=0;i<result.distributeSpecialList.length;i++){
				                		var distributeSpecial = result.distributeSpecialList[i];
				                		tx.executeSql("update Golden45Sales set Golden45s="+distributeSpecial.Other1+",Other1='"+distributeSpecial.Other4+"',Other3='"+distributeSpecial.Other3+"' where CustomerCode="+distributeSpecial.CustomerCode,
					                		[],
					                		function (tx, result) {},
					                		function (tx, error) {}
					                	);
				                	}
				                }
				                if(navigator.localfile && typeof(navigator.localfile.downloadImage) != "undefined"){
				                	if(Ext.timerObj){
					    				clearInterval(Ext.timerObj);
					    			}
				                	util.showLoadMaskMax('16/16' + ' 處理Golden45Sales數據...');
				                	var resultCallback = function(result) {
				                		util.hideLoadMask();
						    			if(isShowHome){
								    		config.redirec.redirectTo('redirec/home');
								    	}
						    		};
							    	navigator.localfile.downloadImage(resultCallback);
						    	} else {
						    		if(Ext.timerObj){
				                		util.hideLoadMask();
					    				clearInterval(Ext.timerObj);
					    			}
						    		if(isShowHome){
							    		config.redirec.redirectTo('redirec/home');
							    	}
						    	}
							}
						);
                	}else{
                		util.showMessageMask(result.msg,'warn-max');
                	}
                },
                failure: function () {
                	util.showMessageMask('連接服務器失敗，請稍後再試','error-max');
                }
            });
		},
		// 客户_品牌进度_查看明细
		brandProgressToView : function(brandCode) {
			var display = document.getElementById(brandCode+"_Detail").style.display;
			for(var i=0;i<config.brand_code_arr.length;i++){
				document.getElementById(config.brand_code_arr[i]+"_Detail").style.display="none";
			}
			if(display == ""){
				document.getElementById(brandCode+"_Detail").style.display="none";
			}else{
				document.getElementById(brandCode+"_Detail").style.display="";
			}
		},
		// 客户_销售进度
		salesProgressView : function(isMonth) {
			util.showLoadMask(null);
			var CustomerCode = config.customer_code.replace("customer_button_","").split("_")[0];
			if(Ext.getCmp("sales-progress-customer-label") && Number(Ext.getCmp("sales-progress-customer-label").getData())>0){
				CustomerCode = Ext.getCmp("sales-progress-customer-label").getData().toString()+"";
			}
			var beginDateStr = "";
			var endDateStr = "";
			if(isMonth == '1'){
				beginDateStr = Ext.getCmp("salesProgressBeginMonth").getComponent('field-value').getData();
				var beginDateArr = beginDateStr.split("-");
				beginDateStr = custom.getMonthFirst(beginDateArr[0],(Number(beginDateArr[1])-1));
    			beginDateStr = beginDateStr.replace(/\-/g,"");
				endDateStr = Ext.getCmp("salesProgressEndMonth").getComponent('field-value').getData();
				var endDateArr = endDateStr.split("-");
				endDateStr = custom.getMonthLast(endDateArr[0],(Number(endDateArr[1])-1));
    			endDateStr = endDateStr.replace(/\-/g,"");
			}else{
				beginDateStr = Ext.getCmp("salesProgressBeginDate").getComponent('field-value').getData();
				endDateStr = Ext.getCmp("salesProgressEndDate").getComponent('field-value').getData();
				beginDateStr = beginDateStr.replace(/\-/g,"");
				endDateStr = endDateStr.replace(/\-/g,"");
			}
			
			var customerObj,salesProgressCntlacctList,salesProgressOptfld4List,salesProgressList;
			config.database.transaction(
		        function (tx) {
		        	tx.executeSql(
		        		"select * from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"'", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				customerObj = r.rows.item(0);
		        			}
		        		},
		        		function (tx, e) {}
		            );
		            if(CustomerCode){
		            	tx.executeSql(
		        			"select CNTLACCT,sum(OCTN+OPC) as Qty,SUM(OrderAmt) as OrderAmt from TAllSalesDataPK " +
								" where SRCode='"+config.current_user.user_name+"' " +
								" AND CUSTOMER = '"+CustomerCode+"' " +
								" and ORDDATE >= '"+beginDateStr+"' " +
								" and ORDDATE <= '"+endDateStr+"' " +
								" group by CNTLACCT", 
			        		[],
			        		function (tx, r) {
			        			if(Number(r.rows.length) > 0){
			        				salesProgressCntlacctList = r.rows;
			        			}
			        		},
			        		function (tx, e) {}
			            );
			          	tx.executeSql(
			        		"select CNTLACCT,OPTFLD4,sum(OCTN+OPC) as Qty,SUM(OrderAmt) as OrderAmt from TAllSalesDataPK " +
								" where SRCode='"+config.current_user.user_name+"' " +
								" AND CUSTOMER = '"+CustomerCode+"' " +
								" and ORDDATE >= '"+beginDateStr+"' " +
								" and ORDDATE <= '"+endDateStr+"' " +
								" group by CNTLACCT,OPTFLD4", 
			        		[],
			        		function (tx, r) {
			        			if(Number(r.rows.length) > 0){
			        				salesProgressOptfld4List = r.rows;
			        			}
			        		},
			        		function (tx, e) {}
			            );
			            tx.executeSql(
			        		"select * from (select CNTLACCT,OPTFLD4,ITEM,DESC,ORDUNIT,sum(OCTN+OPC) as Qty,sum(OrderAmt) as OrderAmt,Round((OrderAmt/(OCTN+OPC)),2) as unitPrice from TAllSalesDataPK " +
								" where SRCode='"+config.current_user.user_name+"' " +
								" AND CUSTOMER = '"+CustomerCode+"' " +
								" and ORDDATE >= '"+beginDateStr+"' " +
								" and ORDDATE <= '"+endDateStr+"' group by CNTLACCT,OPTFLD4,ITEM) r order by r.ITEM ", 
			        		[],
			        		function (tx, r) {
			        			if(Number(r.rows.length) > 0){
			        				salesProgressList = r.rows;
			        			}
			        		},
			        		function (tx, e) {}
			            );
		            }else{
		            	tx.executeSql(
		        			"select CNTLACCT,sum(OCTN+OPC) as Qty,SUM(OrderAmt) as OrderAmt from TAllSalesDataPK " +
								" where SRCode='"+config.current_user.user_name+"' " +
								" and ORDDATE >= '"+beginDateStr+"' " +
								" and ORDDATE <= '"+endDateStr+"' " +
								" group by CNTLACCT", 
			        		[],
			        		function (tx, r) {
			        			if(Number(r.rows.length) > 0){
			        				salesProgressCntlacctList = r.rows;
			        			}
			        		},
			        		function (tx, e) {}
			            );
			          	tx.executeSql(
			        		"select CNTLACCT,OPTFLD4,sum(OCTN+OPC) as Qty,SUM(OrderAmt) as OrderAmt from TAllSalesDataPK " +
								" where SRCode='"+config.current_user.user_name+"' " +
								" and ORDDATE >= '"+beginDateStr+"' " +
								" and ORDDATE <= '"+endDateStr+"' " +
								" group by CNTLACCT,OPTFLD4", 
			        		[],
			        		function (tx, r) {
			        			if(Number(r.rows.length) > 0){
			        				salesProgressOptfld4List = r.rows;
			        			}
			        		},
			        		function (tx, e) {}
			            );
			            tx.executeSql(
			        		"select * from (select CNTLACCT,OPTFLD4,ITEM,DESC,ORDUNIT,sum(OCTN+OPC) as Qty,sum(OrderAmt) as OrderAmt ,Round((OrderAmt/(OCTN+OPC)),2) as unitPrice from TAllSalesDataPK " +
								" where SRCode='"+config.current_user.user_name+"' " +
								" and ORDDATE >= '"+beginDateStr+"' " +
								" and ORDDATE <= '"+endDateStr+"' group by CNTLACCT,OPTFLD4,ITEM) r order by r.ITEM ", 
			        		[],
			        		function (tx, r) {
			        			if(Number(r.rows.length) > 0){
			        				salesProgressList = r.rows;
			        			}
			        		},
			        		function (tx, e) {}
			            );
		            }
		        }
		    );
			setTimeout(function dataShow(){
				if(Ext.getCmp("sales-progress-customer-name")){
					Ext.getCmp("sales-progress-customer-name").setHtml(customerObj["CustomerCode"]+" "+customerObj["CustomerName"]);
				}
				var htmlStr = "";
				var sumHtmlStr = "";
               	Ext.getCmp("sales-progress-data").setHtml(htmlStr);
               	Ext.getCmp("sales-progress-data-sum").setHtml(sumHtmlStr);
				htmlStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;margin-top: -1px;">';
				sumHtmlStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">';
				var qtyTotal=0,orderAmtTotal=0;
				if(salesProgressCntlacctList && Number(salesProgressCntlacctList.length)>0){
					for(var i=0;i<salesProgressCntlacctList.length;i++){
					  	var salesProgressCntlacct = salesProgressCntlacctList.item(i);
						htmlStr += '<tr style="height:2rem;background-color:#FCF4E1;color:#EB5636">'+
							  		'<td style="text-align:center;" width:5%;"><img style="width:0.7rem;height:auto;margin-top:0.2rem;" src="./resources/images/icon_brand.png"/></td>'+
							  		'<td style="border-right:1px solid #CFCFCF;" width:20%;">品牌：'+salesProgressCntlacct["CNTLACCT"]+'</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;width:25%;">&nbsp;</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;width:10%;">&nbsp;</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;width:15%;">&nbsp;</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;width:10%;">'+custom.thousandBitSeparator(salesProgressCntlacct["Qty"],0)+'</td>'+
								    '<td style="padding-right:0.2rem;text-align:right;width:15%;">'+custom.thousandBitSeparator(salesProgressCntlacct["OrderAmt"],0)+'</td>'+
								  '</tr>';
								  if(salesProgressOptfld4List && Number(salesProgressOptfld4List.length)>0){
								  	for(var j=0;j<salesProgressOptfld4List.length;j++){
									   	var salesProgressOptfld4 = salesProgressOptfld4List.item(j);
									   	if(salesProgressOptfld4["CNTLACCT"] == salesProgressCntlacct["CNTLACCT"]){
									   		qtyTotal=Number(Number(qtyTotal)+Number(salesProgressOptfld4["Qty"]));
									   		orderAmtTotal=Number(Number(orderAmtTotal)+Number(salesProgressOptfld4["OrderAmt"]));
										  	htmlStr += '<tr style="height:2rem;font-weight:bold;color:#4C4C4C">'+
													  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.3rem;width:25%;" colspan="2">分類：'+salesProgressOptfld4["OPTFLD4"]+'</td>'+
													    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
													    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
													    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
													    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(salesProgressOptfld4["Qty"],0)+'</td>'+
													    '<td style="padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(salesProgressOptfld4["OrderAmt"],0)+'</td>'+
													  '</tr>';
													  var n = 0;
													  if(salesProgressList && Number(salesProgressList.length)>0){
													  	for(var k=0;k<salesProgressList.length;k++){
														  	var salesProgressObj = salesProgressList.item(k);
														  	if(salesProgressObj["CNTLACCT"] == salesProgressCntlacct["CNTLACCT"] && salesProgressObj["OPTFLD4"] == salesProgressOptfld4["OPTFLD4"]){
														  		if(n%2==0){
														  			htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D">';
														  		}else{
														  			htmlStr += '<tr style="height:2rem;color:#4D4D4D">';
														  		}
															  	htmlStr += ''+
																		  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.3rem;width:25%;" colspan="2">'+salesProgressObj["ITEM"]+'</td>'+
																		    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+salesProgressObj["DESC"]+'</td>'+
																		    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;">'+salesProgressObj["ORDUNIT"]+'</td>'+
																		    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(salesProgressObj["unitPrice"],2)+'</td>'+
																		    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(salesProgressObj["Qty"],0)+'</td>'+
																		    '<td style="padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(salesProgressObj["OrderAmt"],0)+'</td>'+
																		  '</tr>';
																n++;
														  	}
										}
													  }
													  
								}
							}
								  }
								   
					}
					sumHtmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;">'+
								    '<td style="border-right:1px solid #CFCFCF;text-align:center;width:25%;" colspan="2">&nbsp;</td>'+
								    '<td style="border-right:1px solid #CFCFCF;width:25%;padding-left:0.1rem;">&nbsp;</td>'+
								    '<td style="border-right:1px solid #CFCFCF;width:10%;padding-left:0.1rem;">&nbsp;</td>'+
								    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;">總計</td>'+
								    '<td style="border-right:1px solid #CFCFCF;width:10%;padding-left:0.1rem;text-align:right;">'+custom.thousandBitSeparator(qtyTotal,0)+'</td>'+
								    '<td style="border-right:1px solid #CFCFCF;width:15%;padding-left:0.1rem;text-align:right;">'+custom.thousandBitSeparator(orderAmtTotal,0)+'</td>'+
								  '</tr>';
				}
				htmlStr += '</table>';
				sumHtmlStr += '</table>';
                Ext.getCmp("sales-progress-data").setHtml(htmlStr);
                Ext.getCmp("sales-progress-data-sum").setHtml(sumHtmlStr);
                util.hideLoadMask();
			},1500);
		},
		// 客户_Call Card记录
		callCardRecordView : function() {
			var CustomerCode = config.customer_code.replace("customer_button_","").split("_")[0];
			var beginDateStr = Ext.getCmp("callCardRecordBeginDate").getComponent('field-value').getData();
			var endDateStr = Ext.getCmp("callCardRecordEndDate").getComponent('field-value').getData();
			util.showLoadMask(null);
			var date1 = beginDateStr.split("-");
			var date2 = endDateStr.split("-");
			date1 = new Date(date1[0],date1[1],date1[2]);
			date2 = new Date(date2[0],date2[1],date2[2]);
			var days= date2.getTime()-date1.getTime();
			var diffDay = parseInt(days/(1000*60*60*24));
			
			var sql = "";
			for(var i=0 ;i<=diffDay; i++){
				var dateDay = Date.parse(beginDateStr);
				dateDay = dateDay+(i*24*60*60*1000);
				dateDay = new Date(dateDay)
				var dateStr = '';
				var curMonth = parseInt(dateDay.getMonth()+1);
			    dateStr += dateDay.getFullYear();
			    dateStr += '';
				dateStr += curMonth < 10 ? '0'+curMonth : curMonth;
				dateStr += '';
				dateStr += dateDay.getDate() < 10 ? '0'+dateDay.getDate() : dateDay.getDate();
				
				sql += ",SUM(CASE WHEN OrdDate='"+dateStr+"' THEN OCTN+OPC ELSE 0 END) AS Qty"+(i+1);
			}
			var beginDateFormatStr = beginDateStr.replace(/\-/g,"");
			var endDateFormatStr = endDateStr.replace(/\-/g,"");
			var customerObj,callCardRecordCntlacctList,callCardRecordOptfld4List,callCardRecordDescList;
			config.database.transaction(
		        function (tx) {
		        	tx.executeSql(
		        		"select * from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"'", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				customerObj = r.rows.item(0);
		        			}
		        		},
		        		function (tx, e) {}
		            );
		            tx.executeSql(
	        			"select CNTLACCT"+sql+" from TAllSalesDataPK " +
							" where SRCode='"+config.current_user.user_name+"' " +
							" AND CUSTOMER = '"+CustomerCode+"' " +
							" and ORDDATE >= '"+beginDateFormatStr+"' " +
							" and ORDDATE <= '"+endDateFormatStr+"' " +
							" group by CNTLACCT", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				callCardRecordCntlacctList = r.rows;
		        			}
		        		},
		        		function (tx, e) {}
		            );
		          	tx.executeSql(
		        		"select CNTLACCT,OPTFLD4"+sql+" from TAllSalesDataPK " +
							" where SRCode='"+config.current_user.user_name+"' " +
							" AND CUSTOMER = '"+CustomerCode+"' " +
							" and ORDDATE >= '"+beginDateFormatStr+"' " +
							" and ORDDATE <= '"+endDateFormatStr+"' " +
							" group by CNTLACCT,OPTFLD4", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				callCardRecordOptfld4List = r.rows;
		        			}
		        		},
		        		function (tx, e) {}
		            );
		            tx.executeSql(
		        		"select CNTLACCT,OPTFLD4,DESC"+sql+" from TAllSalesDataPK " +
							" where SRCode='"+config.current_user.user_name+"' " +
							" AND CUSTOMER = '"+CustomerCode+"' " +
							" and ORDDATE >= '"+beginDateFormatStr+"' " +
							" and ORDDATE <= '"+endDateFormatStr+"' " +
							" group by CNTLACCT,OPTFLD4,DESC", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				callCardRecordDescList = r.rows;
		        			}
		        		},
		        		function (tx, e) {}
		            );
		        }
		    );
			setTimeout(function dataShow(){
				Ext.getCmp("call-card-record-customer-name").setHtml(customerObj["CustomerCode"]+" "+customerObj["CustomerName"]);
        		var beginDateStr = Ext.getCmp("callCardRecordBeginDate").getComponent('field-value').getData();
				var endDateStr = Ext.getCmp("callCardRecordEndDate").getComponent('field-value').getData();
            	var date1 = beginDateStr.split("-");
				var date2 = endDateStr.split("-");
				date1 = new Date(date1[0],date1[1],date1[2]);
				date2 = new Date(date2[0],date2[1],date2[2]);
				var days= date2.getTime()-date1.getTime();
				var diffDay = parseInt(days/(1000*60*60*24));
				var htmlStr = "";
				Ext.getCmp("call-card-record-data").setHtml(htmlStr);
				htmlStr += '<table border="0" cellpadding="0" cellspacing="0" style="width:'+(12.5+(diffDay*2.125))+'rem;border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;border-left:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">'+
							  '<tr style="height:2rem;text-align:center;">'+
							    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:9.313rem;" colspan="2">日期</td>';
								for(var i=0 ;i<=diffDay; i++){
									var dateDay = Date.parse(beginDateStr);
									dateDay = dateDay+(i*24*60*60*1000);
									dateDay = new Date(dateDay)
									var dateDayStr = dateDay.getDate();
									htmlStr += '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:2.125rem;">'+dateDayStr+'</td>';
								}
        			htmlStr += '</tr>';
        		if(callCardRecordCntlacctList  && Number(callCardRecordCntlacctList.length)>0){
        			for(var j=0;j<callCardRecordCntlacctList.length;j++){
	    				var cntlacctMap = callCardRecordCntlacctList.item(j);
	        			var cntlacctKeyArr = new Array()
	        			for(var td in cntlacctMap){
	        				if(td != 'CNTLACCT'){
	        					cntlacctKeyArr.push(td);
	        				}
	        			}
	        			cntlacctKeyArr.sort(function(a,b){return a-b});
	        			htmlStr += '<tr style="height:2rem;background-color:#FCF4E1;color:#EB5636">'+
							  		'<td style="text-align:center;" width:1.688rem;"><img style="width:0.7rem;height:auto;margin-top:0.2rem;" src="./resources/images/icon_brand.png"/></td>'+
							  		'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" width:7.625rem;">品牌：'+cntlacctMap['CNTLACCT']+'</td>';
							  		for(var i=0;i<cntlacctKeyArr.length;i++){
							  			if(cntlacctMap['Qty'+(i+1)]){
							  				htmlStr += '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+cntlacctMap['Qty'+(i+1)]+'</td>';
							  			}else{
							  				htmlStr += '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>';
							  			}
							  		}
						htmlStr += '</tr>';
						if(callCardRecordOptfld4List  && Number(callCardRecordOptfld4List.length)>0){
							for(var k=0;k<callCardRecordOptfld4List.length;k++){
		        				var optfld4Map = callCardRecordOptfld4List.item(k);
		        				if(cntlacctMap['CNTLACCT'] == optfld4Map['CNTLACCT']){
		        					var optfld4KeyArr = new Array()
		                			for(var td in optfld4Map){
		                				if(td != 'CNTLACCT' && td != 'OPTFLD4'){
		                					optfld4KeyArr.push(td);
		                				}
		                			}
		                			optfld4KeyArr.sort(function(a,b){return a-b});
		                			htmlStr += '<tr style="height:2rem;font-weight:bold;color:#4C4C4C">'+
										  		'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" width:7.625rem;" colspan="2">分類：'+optfld4Map['OPTFLD4']+'</td>';
										  		for(var i=0;i<optfld4KeyArr.length;i++){
										  			if(optfld4Map['Qty'+(i+1)]){
										  				htmlStr += '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+optfld4Map['Qty'+(i+1)]+'</td>';
										  			}else{
										  				htmlStr += '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>';
										  			}
										  		}
									htmlStr += '</tr>';
									var n = 0;
									if(callCardRecordDescList  && Number(callCardRecordDescList.length)>0){
										for(var p=0;p<callCardRecordDescList.length;p++){
			                				var descMap = callCardRecordDescList.item(p);
			                				if(optfld4Map['CNTLACCT'] == descMap['CNTLACCT'] && optfld4Map['OPTFLD4'] == descMap['OPTFLD4']){
			                					var descKeyArr = new Array()
				                    			for(var td in descMap){
				                    				if(td != 'CNTLACCT' && td != 'OPTFLD4' && td != 'DESC'){
				                    					descKeyArr.push(td);
				                    				}
				                    			}
				                    			descKeyArr.sort(function(a,b){return a-b});
				                    			if(n%2==0){
				                    				htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D">';
				                    			}else{
				                    				htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;color:#4D4D4D">';
				                    			}
				                    			htmlStr += ''+
													  		'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" width:7.625rem;" colspan="2">'+descMap['DESC']+'</td>';
													  		for(var i=0;i<descKeyArr.length;i++){
													  			if(descMap['Qty'+(i+1)]){
													  				htmlStr += '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+descMap['Qty'+(i+1)]+'</td>';
													  			}else{
													  				htmlStr += '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>';
													  			}
													  		}
												htmlStr += '</tr>';
												n++;
			                				}
			                			}
									}
		        				}
		        			}
						}
	    			}
        		}
    			htmlStr += '</table>';
               	Ext.getCmp("call-card-record-data").setHtml(htmlStr);
				util.hideLoadMask();
			},1500);
		},
		// 客户_本月销售进度_查看明细
		thisMonthSalesProgressToView : function(brandCode) {
			var display = document.getElementById(brandCode+"_Sales_Progress_Detail").style.display;
			for(var i=0;i<config.brand_code_arr.length;i++){
				document.getElementById(config.brand_code_arr[i]+"_Sales_Progress_Detail").style.display="none";
			}
			if(display == ""){
				document.getElementById(brandCode+"_Sales_Progress_Detail").style.display="none";
			}else{
				document.getElementById(brandCode+"_Sales_Progress_Detail").style.display="";
			}
		},
		// 客户_本月引进进度_查看明细
		thisMonthIntroductionProgressToView : function(areaCode) {
			var display = document.getElementById(areaCode+"_tr").style.display;
			for(var i=0;i<config.area_code_arr.length;i++){
				document.getElementById(config.area_code_arr[i]+"_tr").style.display="none";
			}
			config.database.transaction(
	            function (tx) {
	            	tx.executeSql("select AreaCode, AreaName, CustomerCode, CustomerName, (case when IsGolden45>0 then Golden45s else 'NA' end) as Golden45, " +
							"Orders, Other1, Other2 from Golden45Sales where AreaCode="+areaCode+" order by AreaCode, Orders, CustomerCode", 
	                    [],
	                    function (tx, result) {
	                    	var htmlStr = '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="color:#4D4D4D;">';
	                    	for (var j = 0; j < result.rows.length; j++) {
	                    		if(Number(result.rows.item(j)['Orders'])>0){
	                    			htmlStr += '<tr style="height:1.6rem;background-color:#E1E1E1;color:#0080FF" onclick="javascript:custom.toCustomerInfo(\''+result.rows.item(j)['CustomerCode']+'\');">';
	                    		}else {
	                    			htmlStr += '<tr style="height:1.6rem;background-color:#E1E1E1;color:#DE3F33" onclick="javascript:custom.toCustomerInfo(\''+result.rows.item(j)['CustomerCode']+'\');">';
	                    		}
	                    			htmlStr += ''+
								            '<td style="width:20%;padding-left:0.2rem;">'+result.rows.item(j)['CustomerCode']+'</td>'+
								            '<td style="width:16%;padding-left:0.5rem;">'+result.rows.item(j)['Golden45']+'</td>'+
								            '<td style="width:16%;padding-left:0.5rem;">'+result.rows.item(j)['Other2']+'</td>'+
								            '<td style="width:48%;padding-left:0.2rem;">'+result.rows.item(j)['CustomerName']+'</td>'+
								          '</tr>';
	                    	}
		                      htmlStr += '</table>';
		                      document.getElementById(areaCode+"_td").innerHTML = htmlStr;
		                    if(display == ""){
								document.getElementById(areaCode+"_tr").style.display="none";
							}else{
								document.getElementById(areaCode+"_tr").style.display="";
							}
	                    },
	                    function (tx, error) {
	                        
	                    }
	                );
	            }
	        );
		},
		// 客户_信息
		toCustomerInfo : function(CustomerCode) {
			config.customer_code = "customer_button_"+CustomerCode+"_"+config.current_user.user_name;
			Ext.getCmp("titleBar").setActiveItem(1);
			Ext.getCmp("customerHome").removeAll();
			Ext.getCmp("customerHome").add({
				xtype : 'customerInfo'
			});
		},
		// 客户_订单记录
		orderRecordReportView : function(orderType) {
			var CustomerCode = config.customer_code.replace("customer_button_","").split("_")[0];
			if(Ext.getCmp("order-record-customer-label") && Number(Ext.getCmp("order-record-customer-label").getData())>0){
				CustomerCode = Ext.getCmp("order-record-customer-label").getData().toString()+"";
			}
			var addressStr = Ext.getCmp("order-record-customer-address").getData().toString().replace("~~~","");
			var beginDateStr = Ext.getCmp("orderRecordBeginDate").getComponent('field-value').getData();
			var endDateStr = Ext.getCmp("orderRecordEndDate").getComponent('field-value').getData();
			if(Number(orderType) == 3){
				var whereStr = "";
				if(CustomerCode && CustomerCode!=""){
					whereStr += " and CustomerCode='"+CustomerCode+"'";
				}
				if(beginDateStr && beginDateStr!=""){
					whereStr += " and OrderDate>='"+beginDateStr+"'";
				}
				if(endDateStr && endDateStr!=""){
					whereStr += " and OrderDate<='"+endDateStr+"'";
				}
				if(addressStr && addressStr!=""){
					whereStr += " and AddressStr='"+addressStr+"'";
				}
				config.database.transaction(
			        function (tx) {
			        	tx.executeSql("select * from OrderHead where SalesCode='"+config.current_user.user_name+"' "+whereStr+" order by OrderNo", [],
			                function (tx, result) {
			                	var htmlStr = "";
	                    		Ext.getCmp("order-record-data").setHtml(htmlStr);
	                    		htmlStr += 	'<table id="orderRecordDataTable" border="0" cellpadding="0" cellspacing="0" style="width:40rem;border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;border-left:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">'+
											  '<tr style="height:2rem;text-align:center;">'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">訂單號</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">客戶號碼</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:6.5rem;">客戶名稱</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">訂單日期</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">送貨日期</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">付款方式</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:2.5rem;">項數</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4rem;">項額</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:12rem;">送貨地址</td>'+
											  '</tr>';
								config.ord_number_arr = [];
								var qtyTotal=0,totalAmountTotal=0;
	                    		for(var j=0;j<result.rows.length;j++){
	                    			var orderRecord = result.rows.item(j);
	                    			qtyTotal=Number(Number(qtyTotal)+1);
	                    			totalAmountTotal=Number(Number(totalAmountTotal)+Number(orderRecord['TotalAmount']));
	                    			config.ord_number_arr.push(orderRecord['OrderNo']);
	                    			if(j%2==0){
		                				htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D" onclick="javascript:custom.localOrderToView(\''+orderRecord['OrderNo']+'\');">';
		                			}else{
		                				htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;color:#4D4D4D" onclick="javascript:custom.localOrderToView(\''+orderRecord['OrderNo']+'\');">';
		                			}
		                			var PaymentStr = "";
		                			if(Number(orderRecord['Payment']) == 1){
		                				PaymentStr = "月結";
		                			}else if(Number(orderRecord['Payment']) == 1){
		                				PaymentStr = "現款";
		                			}else {
		                				PaymentStr = "其他";
		                			}
		                			htmlStr +=  '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['OrderNo']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['CustomerCode']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['CustomerName']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['OrderDate']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['DeliveryDate']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+PaymentStr+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+custom.thousandBitSeparator(orderRecord['DetailCount'],0)+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">$'+custom.thousandBitSeparator(orderRecord['TotalAmount'],0)+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['AddressStr']+'</td>'+
											  '</tr>'+
											  '<tr style="display:none;" id="'+orderRecord['OrderNo']+'_Order_Detail">'+
											  	'<td colspan="9">'+
												'</td>'+
											  '</tr>';
	                    		}
	                    		
	                    		htmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;border-top:1px solid #CFCFCF;">'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;" colspan="2">訂單總數</td>'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" colspan="3">'+custom.thousandBitSeparator(qtyTotal,0)+'</td>'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;" colspan="2">總額</td>'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" colspan="2">$'+custom.thousandBitSeparator(totalAmountTotal,0)+'</td>'+
											  '</tr>';
	                    		htmlStr += 	'</table>';
	                    		Ext.getCmp("order-record-data").setHtml(htmlStr);			  
			                },
			                function (tx, error){
			                	
			                }
			            );
			        }
			    );
			}else{
				util.showLoadMask(null);
				var whereStr = "";
				var tmpCustomerCodeStr = "";
				var CustomerCodeStr = "";
				var orderHeadCustomerCodeStr = "";
				if(CustomerCode && CustomerCode!=""){
					whereStr += " and CUSTOMER='"+CustomerCode+"'";
					tmpCustomerCodeStr += " and tmp.CustomerCode='"+CustomerCode+"'";
					CustomerCodeStr += " and CustomerCode='"+CustomerCode+"'";
					orderHeadCustomerCodeStr += " and orderHead.CustomerCode='"+CustomerCode+"'";
				}
				if(addressStr && addressStr!=""){
					whereStr += " and ShipAddress like '%"+addressStr.split(" ")[1]+"%'";
					orderHeadCustomerCodeStr += " and orderHead.Address='"+Number(addressStr.split(" ")[0])+"'";
				}
				var beginDateFormatStr = beginDateStr.replace(/\-/g,"");
				var endDateFormatStr = endDateStr.replace(/\-/g,"");
				var customerObj,orderRecordList,orderRecordDetailList;
				if(Number(orderType) == 1){
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				customerObj = r.rows.item(0);
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        	tx.executeSql(
				        		"select ORDNUMBER as OrdNumber, max(CUSTOMER) as customer2, max(BILNAME) as bilname2, " +
								" max(ORDDATE) as OrdDate2, count(*) as DetailCount, sum(OrderAmt) as Amt, " +
								" max(CASE WHEN ShipTo='' THEN '001' ELSE ShipTo END) as ShipTo,max(ShipAddress) as ShipAddress, " +
								" '' as DeliveryDate,'其他' as Payment " +
								" from TAllSalesDataPK " +
								" where OrdType=0  " +
								" and ORDDATE >= '"+beginDateFormatStr+"' " +
								" and ORDDATE <= '"+endDateFormatStr+"' " +
									whereStr +
								" and SRCode = '"+config.current_user.user_name+"' " +
								" group by ORDNUMBER " +
								" order by ORDNUMBER ", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				            tx.executeSql(
				        		"select ORDNUMBER as OrdNumber, ITEM as item, max(LINENUM)/32 as DetailNo, max(DESC) as desc2, " +
								" max(ORDUNIT) as OrdUnit2, sum(OCTN + OPC) as Qty, max(UNITPRICE) as UnitPrice2, sum(OrderAmt) as Amt " +
								" from TAllSalesDataPK" +
								" where OrdType=0 " +
								" and ORDDATE >= '"+beginDateFormatStr+"' " +
								" and ORDDATE <= '"+endDateFormatStr+"' " +
									whereStr +
								" and SRCode = '"+config.current_user.user_name+"' " +
								" group by ORDNUMBER, ITEM " +
								" order by ORDNUMBER, max(LINENUM)/32", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordDetailList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
				} else if(Number(orderType) == 2){
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				customerObj = r.rows.item(0);
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        	tx.executeSql(
				        		"select orderHead.OrderNo as OrdNumber, " +
							  	" CustomerInfo.CustomerCode as customer2," +
							  	" CustomerInfo.CustomerName as bilname2," +
								" orderHead.OrderDate as OrdDate2," +
								" orderHead.DeliveryDate as DeliveryDate," +
								" case Payment when 1 then '月結' when 2 then '現款' else '其他' end as Payment," +
								" orderHead.DetailCount as DetailCount," +
								" orderHead.TotalAmount as Amt," +
								" (customerAddress.Address1+';'+customerAddress.Address2+';'+customerAddress.Address3+';'+customerAddress.Address4) as ShipTo2" +
								" from FOrderHead orderHead left join (" +
									" select tmp.* from (select CustomerCode,SalesCode, '001' as IDCUSTSHPT, Address1, Address2, Address3, '' as Address4" +
									 " from Customer" +
									 " union all" +
									 " select IDCUST as CustomerCode,SalesCode, IDCUSTSHPT, TEXTSTRE1 as Address1, TEXTSTRE2 as Address2, TEXTSTRE3 as Address3, TEXTSTRE4 as Address4" +
									 " from CustomerAddress" +
									" ) tmp " +
									" where tmp.SalesCode = '"+config.current_user.user_name+"'" +
										tmpCustomerCodeStr +
								" ) customerAddress on orderHead.CustomerCode = customerAddress.CustomerCode and orderHead.Address=CAST(customerAddress.IDCUSTSHPT as int)" +
								" left join (" +
									" select CustomerCode,CustomerName from Customer " +
									" where SalesCode='"+config.current_user.user_name+"'" +
										CustomerCodeStr +
								" ) CustomerInfo on orderHead.CustomerCode = CustomerInfo.CustomerCode" +
								" where " +
								" orderHead.OrderDate >= '"+beginDateFormatStr+"'" +
								" and orderHead.OrderDate <= '"+endDateFormatStr+"'" +
									orderHeadCustomerCodeStr +
								" and orderHead.SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				            tx.executeSql(
				        		"select detail.OrderNo as OrdNumber, " +
								" detail.DetailNo as DetailNo," +
								" detail.ItemCode as item," +
								" item.Unit as OrdUnit2," +
								" detail.OrderQty as Qty," +
								" detail.Price as UnitPrice2," +
								" detail.SubTotal as Amt," +
								" item.ItemName as desc2 from FOrderDetail detail left join Item item on detail.ItemCode=item.ItemCode " +
								" where item.SalesCode='"+config.current_user.user_name+"' " +
								" and detail.OrderNo in (" +
									" select OrderNo " +
									" from FOrderHead " +
									" where " +
									" OrderDate >= '"+beginDateFormatStr+"'" +
									" and OrderDate <= '"+endDateFormatStr+"'" +
										CustomerCodeStr +
									" and SalesCode='"+config.current_user.user_name+"'" +
								" ) order by detail.DetailNo ", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordDetailList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
				}
				setTimeout(function dataShow(){
					if(Ext.getCmp("order-record-customer-name") && customerObj){
        				Ext.getCmp("order-record-customer-name").setHtml(customerObj["CustomerCode"]+" "+customerObj["CustomerName"]);
        			}
        			var htmlStr = "";
            		Ext.getCmp("order-record-data").setHtml(htmlStr);
	                htmlStr += 	'<table border="0" cellpadding="0" cellspacing="0" style="width:49.9rem;border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;border-left:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">'+
								  '<tr style="height:2rem;text-align:center;">'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">訂單號</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.5rem;">客戶號碼</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:6.5rem;">客戶名稱</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">訂單日期</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">送貨日期</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">付款方式</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:2.5rem;">項數</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:6rem;">項額</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:15rem;">送貨地址</td>'+
								  '</tr>';
					config.ord_number_arr = [];
					var qtyTotal=0,totalAmountTotal=0;
					if(orderRecordList && Number(orderRecordList.length)>0){
						for(var j=0;j<orderRecordList.length;j++){
	            			var orderRecord = orderRecordList.item(j);
	            			qtyTotal=Number(Number(qtyTotal)+1);
                			totalAmountTotal=Number(Number(totalAmountTotal)+Number(orderRecord['Amt']));
                			var OrdDateStr = orderRecord["OrdDate2"];
                			var DeliveryDateStr = orderRecord["DeliveryDate"];
                			var OrdDate2="",DeliveryDate="";
                			if(OrdDateStr !=null && OrdDateStr != ""){
                				OrdDate2 = OrdDateStr.substr(0,4)+"-"+OrdDateStr.substr(4,2)+"-"+OrdDateStr.substr(6,2);
                			}
                			if(DeliveryDateStr !=null && DeliveryDateStr != ""){
                				DeliveryDate = DeliveryDateStr.substr(0,4)+"-"+DeliveryDateStr.substr(4,2)+"-"+DeliveryDateStr.substr(6,2);
                			}
	            			config.ord_number_arr.push(orderRecord["OrdNumber"]);
	            			if(j%2==0){
	            				htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D" onclick="javascript:custom.orderToView(\''+orderRecord["OrdNumber"]+'\');">';
	            			}else{
	            				htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;color:#4D4D4D" onclick="javascript:custom.orderToView(\''+orderRecord["OrdNumber"]+'\');">';
	            			}
	                			htmlStr +=  '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["OrdNumber"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["customer2"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["bilname2"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+OrdDate2+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+DeliveryDate+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["Payment"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;padding-right:0.2rem;text-align:right;">'+orderRecord["DetailCount"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;padding-right:0.2rem;text-align:right;">$'+custom.thousandBitSeparator(orderRecord["Amt"],0)+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["ShipTo"]+' '+orderRecord["ShipAddress"]+'</td>'+
										  '</tr>'+
										  '<tr style="display:none;" id="'+orderRecord["OrdNumber"]+'_Order_Detail">'+
										  	'<td colspan="9">'+
										  		'<table border="0" cellpadding="0" cellspacing="0" style="width:49.9rem;color:#4D4D4D;">'+
										          '<tr style="height:1.6rem;background-color:#E1E1E1;font-weight:bold;text-align:center;">'+
										            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">序號</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:4.5rem;">貨品編號</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:10.7rem;">貨品名稱</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:4.2rem;">單位</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">單價</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:2.5rem;">數量</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:6rem;">項計</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:15rem;">&nbsp;</td>'+
										          '</tr>';
										          if(orderRecordDetailList && Number(orderRecordDetailList.length)>0){
										          	  for(var k=0;k<orderRecordDetailList.length;k++){
											          	  var orderRecordDetail = orderRecordDetailList.item(k);
											          	  if(orderRecordDetail["OrdNumber"] == orderRecord["OrdNumber"]){
											          	  	  htmlStr +=  '<tr style="height:1.6rem;background-color:#E1E1E1;">'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;">'+orderRecordDetail["DetailNo"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["item"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["desc2"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;">'+orderRecordDetail["OrdUnit2"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(orderRecordDetail["UnitPrice2"],2)+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;padding-right:0.2rem;text-align:right;">'+orderRecordDetail["Qty"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;padding-right:0.2rem;text-align:right;">$'+custom.thousandBitSeparator(orderRecordDetail["Amt"],0)+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
																          '</tr>';
											          	  }
											          }
										          }
						        htmlStr +=  '</table>'+
							'</td>'+
						  '</tr>';
	            		}
					}
					htmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;border-top:1px solid #CFCFCF;">'+
								    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;" colspan="2">訂單總數</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;" colspan="3">'+custom.thousandBitSeparator(qtyTotal,0)+'</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;" colspan="2">總額</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;" colspan="2">$'+custom.thousandBitSeparator(totalAmountTotal,0)+'</td>'+
								  '</tr>';
					htmlStr += 	'</table>';
	                Ext.getCmp("order-record-data").setHtml(htmlStr);
					util.hideLoadMask();
				},1500);
			}
		},
		// 客户_订单记录
		orderRecordView : function(orderType) {
			var CustomerCode = config.customer_code.replace("customer_button_","").split("_")[0];
			if(Ext.getCmp("order-record-customer-label") && Number(Ext.getCmp("order-record-customer-label").getData())>0){
				CustomerCode = Ext.getCmp("order-record-customer-label").getData().toString()+"";
			}
			var addressStr = Ext.getCmp("order-record-customer-address").getData().toString().replace("~~~","");
			var beginDateStr = Ext.getCmp("orderRecordBeginDate").getComponent('field-value').getData();
			var endDateStr = Ext.getCmp("orderRecordEndDate").getComponent('field-value').getData();
			if(Number(orderType) == 3){
				var whereStr = "";
				if(CustomerCode && CustomerCode!=""){
					whereStr += " and CustomerCode='"+CustomerCode+"'";
				}
				if(beginDateStr && beginDateStr!=""){
					whereStr += " and OrderDate>='"+beginDateStr+"'";
				}
				if(endDateStr && endDateStr!=""){
					whereStr += " and OrderDate<='"+endDateStr+"'";
				}
				if(addressStr && addressStr!=""){
					whereStr += " and AddressStr='"+addressStr+"'";
				}
				config.database.transaction(
			        function (tx) {
			        	tx.executeSql("select * from OrderHead where SalesCode='"+config.current_user.user_name+"' "+whereStr+" order by OrderNo", [],
			                function (tx, result) {
			                	var htmlStr = "";
	                    		Ext.getCmp("order-record-data").setHtml(htmlStr);
	                    		htmlStr += 	'<table id="orderRecordDataTable" border="0" cellpadding="0" cellspacing="0" style="width:40rem;border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;border-left:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">'+
											  '<tr style="height:2rem;text-align:center;">'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">訂單號</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:2.5rem;">項數</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">客戶號碼</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:6.5rem;">客戶名稱</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">訂單日期</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">送貨日期</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">付款方式</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4rem;">項額</td>'+
											    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:12rem;">送貨地址</td>'+
											  '</tr>';
								config.ord_number_arr = [];
								var qtyTotal=0,totalAmountTotal=0;
	                    		for(var j=0;j<result.rows.length;j++){
	                    			var orderRecord = result.rows.item(j);
	                    			qtyTotal=Number(Number(qtyTotal)+1);
	                    			totalAmountTotal=Number(Number(totalAmountTotal)+Number(orderRecord['TotalAmount']));
	                    			config.ord_number_arr.push(orderRecord['OrderNo']);
	                    			if(j%2==0){
		                				htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D" onclick="javascript:custom.localOrderToView(\''+orderRecord['OrderNo']+'\');">';
		                			}else{
		                				htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;color:#4D4D4D" onclick="javascript:custom.localOrderToView(\''+orderRecord['OrderNo']+'\');">';
		                			}
		                			var PaymentStr = "";
		                			if(Number(orderRecord['Payment']) == 1){
		                				PaymentStr = "月結";
		                			}else if(Number(orderRecord['Payment']) == 1){
		                				PaymentStr = "現款";
		                			}else {
		                				PaymentStr = "其他";
		                			}
		                			htmlStr +=  '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['OrderNo']+'</td>'+
		                						'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+custom.thousandBitSeparator(orderRecord['DetailCount'],0)+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['CustomerCode']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['CustomerName']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['OrderDate']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['DeliveryDate']+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+PaymentStr+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">$'+custom.thousandBitSeparator(orderRecord['TotalAmount'],0)+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord['AddressStr']+'</td>'+
											  '</tr>'+
											  '<tr style="display:none;" id="'+orderRecord['OrderNo']+'_Order_Detail">'+
											  	'<td colspan="9">'+
												'</td>'+
											  '</tr>';
	                    		}
	                    		
	                    		htmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;border-top:1px solid #CFCFCF;">'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;" colspan="2">訂單總數</td>'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" colspan="3">'+custom.thousandBitSeparator(qtyTotal,0)+'</td>'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;" colspan="2">總額</td>'+
											    '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;" colspan="2">$'+custom.thousandBitSeparator(totalAmountTotal,0)+'</td>'+
											  '</tr>';
	                    		htmlStr += 	'</table>';
	                    		Ext.getCmp("order-record-data").setHtml(htmlStr);			  
			                },
			                function (tx, error){
			                	
			                }
			            );
			        }
			    );
			}else{
				util.showLoadMask(null);
				var whereStr = "";
				var tmpCustomerCodeStr = "";
				var CustomerCodeStr = "";
				var orderHeadCustomerCodeStr = "";
				if(CustomerCode && CustomerCode!=""){
					whereStr += " and CUSTOMER='"+CustomerCode+"'";
					tmpCustomerCodeStr += " and tmp.CustomerCode='"+CustomerCode+"'";
					CustomerCodeStr += " and CustomerCode='"+CustomerCode+"'";
					orderHeadCustomerCodeStr += " and orderHead.CustomerCode='"+CustomerCode+"'";
				}
				if(addressStr && addressStr!=""){
					whereStr += " and ShipAddress like '%"+addressStr.split(" ")[1]+"%'";
					orderHeadCustomerCodeStr += " and orderHead.Address='"+Number(addressStr.split(" ")[0])+"'";
				}
				var beginDateFormatStr = beginDateStr.replace(/\-/g,"");
				var endDateFormatStr = endDateStr.replace(/\-/g,"");
				var customerObj,orderRecordList,orderRecordDetailList;
				if(Number(orderType) == 1){
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				customerObj = r.rows.item(0);
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        	tx.executeSql(
				        		"select ORDNUMBER as OrdNumber, max(CUSTOMER) as customer2, max(BILNAME) as bilname2, " +
								" max(ORDDATE) as OrdDate2, count(*) as DetailCount, sum(OrderAmt) as Amt, " +
								" max(CASE WHEN ShipTo='' THEN '001' ELSE ShipTo END) as ShipTo,max(ShipAddress) as ShipAddress, " +
								" '' as DeliveryDate,'其他' as Payment " +
								" from TAllSalesDataPK " +
								" where OrdType=0  " +
								" and ORDDATE >= '"+beginDateFormatStr+"' " +
								" and ORDDATE <= '"+endDateFormatStr+"' " +
									whereStr +
								" and SRCode = '"+config.current_user.user_name+"' " +
								" group by ORDNUMBER " +
								" order by ORDNUMBER ", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				            tx.executeSql(
				        		"select ORDNUMBER as OrdNumber, ITEM as item, max(LINENUM)/32 as DetailNo, max(DESC) as desc2, " +
								" max(ORDUNIT) as OrdUnit2, sum(OCTN + OPC) as Qty, max(UNITPRICE) as UnitPrice2, sum(OrderAmt) as Amt " +
								" from TAllSalesDataPK" +
								" where OrdType=0 " +
								" and ORDDATE >= '"+beginDateFormatStr+"' " +
								" and ORDDATE <= '"+endDateFormatStr+"' " +
									whereStr +
								" and SRCode = '"+config.current_user.user_name+"' " +
								" group by ORDNUMBER, ITEM " +
								" order by ORDNUMBER, max(LINENUM)/32", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordDetailList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
				} else if(Number(orderType) == 2){
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				customerObj = r.rows.item(0);
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        	tx.executeSql(
				        		"select orderHead.OrderNo as OrdNumber, " +
							  	" CustomerInfo.CustomerCode as customer2," +
							  	" CustomerInfo.CustomerName as bilname2," +
								" orderHead.OrderDate as OrdDate2," +
								" orderHead.DeliveryDate as DeliveryDate," +
								" case Payment when 1 then '月結' when 2 then '現款' else '其他' end as Payment," +
								" orderHead.DetailCount as DetailCount," +
								" orderHead.TotalAmount as Amt," +
								" (customerAddress.Address1+';'+customerAddress.Address2+';'+customerAddress.Address3+';'+customerAddress.Address4) as ShipTo2" +
								" from FOrderHead orderHead left join (" +
									" select tmp.* from (select CustomerCode,SalesCode, '001' as IDCUSTSHPT, Address1, Address2, Address3, '' as Address4" +
									 " from Customer" +
									 " union all" +
									 " select IDCUST as CustomerCode,SalesCode, IDCUSTSHPT, TEXTSTRE1 as Address1, TEXTSTRE2 as Address2, TEXTSTRE3 as Address3, TEXTSTRE4 as Address4" +
									 " from CustomerAddress" +
									" ) tmp " +
									" where tmp.SalesCode = '"+config.current_user.user_name+"'" +
										tmpCustomerCodeStr +
								" ) customerAddress on orderHead.CustomerCode = customerAddress.CustomerCode and orderHead.Address=CAST(customerAddress.IDCUSTSHPT as int)" +
								" left join (" +
									" select CustomerCode,CustomerName from Customer " +
									" where SalesCode='"+config.current_user.user_name+"'" +
										CustomerCodeStr +
								" ) CustomerInfo on orderHead.CustomerCode = CustomerInfo.CustomerCode" +
								" where " +
								" orderHead.OrderDate >= '"+beginDateFormatStr+"'" +
								" and orderHead.OrderDate <= '"+endDateFormatStr+"'" +
									orderHeadCustomerCodeStr +
								" and orderHead.SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				            tx.executeSql(
				        		"select detail.OrderNo as OrdNumber, " +
								" detail.DetailNo as DetailNo," +
								" detail.ItemCode as item," +
								" item.Unit as OrdUnit2," +
								" detail.OrderQty as Qty," +
								" detail.Price as UnitPrice2," +
								" detail.SubTotal as Amt," +
								" item.ItemName as desc2 from FOrderDetail detail left join Item item on detail.ItemCode=item.ItemCode " +
								" where item.SalesCode='"+config.current_user.user_name+"' " +
								" and detail.OrderNo in (" +
									" select OrderNo " +
									" from FOrderHead " +
									" where " +
									" OrderDate >= '"+beginDateFormatStr+"'" +
									" and OrderDate <= '"+endDateFormatStr+"'" +
										CustomerCodeStr +
									" and SalesCode='"+config.current_user.user_name+"'" +
								" ) order by detail.DetailNo ", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordDetailList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
				}
				setTimeout(function dataShow(){
					if(Ext.getCmp("order-record-customer-name") && customerObj){
        				Ext.getCmp("order-record-customer-name").setHtml(customerObj["CustomerCode"]+" "+customerObj["CustomerName"]);
        			}
        			var htmlStr = "";
            		Ext.getCmp("order-record-data").setHtml(htmlStr);
	                htmlStr += 	'<table border="0" cellpadding="0" cellspacing="0" style="width:31.9rem;border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;border-left:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">'+
								  '<tr style="height:2rem;text-align:center;">'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.5rem;">訂單號</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:2.5rem;">項數</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:10rem;">送貨地址</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">訂單日期</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">送貨日期</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">付款方式</td>'+
								    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3rem;">項額</td>'+
								    
								  '</tr>';
					config.ord_number_arr = [];
					var qtyTotal=0,totalAmountTotal=0;
					if(orderRecordList && Number(orderRecordList.length)>0){
						for(var j=0;j<orderRecordList.length;j++){
	            			var orderRecord = orderRecordList.item(j);
	            			qtyTotal=Number(Number(qtyTotal)+1);
                			totalAmountTotal=Number(Number(totalAmountTotal)+Number(orderRecord['Amt']));
                			var OrdDateStr = orderRecord["OrdDate2"];
                			var DeliveryDateStr = orderRecord["DeliveryDate"];
                			var OrdDate2="",DeliveryDate="";
                			if(OrdDateStr !=null && OrdDateStr != ""){
                				OrdDate2 = OrdDateStr.substr(0,4)+"-"+OrdDateStr.substr(4,2)+"-"+OrdDateStr.substr(6,2);
                			}
                			if(DeliveryDateStr !=null && DeliveryDateStr != ""){
                				DeliveryDate = DeliveryDateStr.substr(0,4)+"-"+DeliveryDateStr.substr(4,2)+"-"+DeliveryDateStr.substr(6,2);
                			}
	            			config.ord_number_arr.push(orderRecord["OrdNumber"]);
	            			if(j%2==0){
	            				htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D" onclick="javascript:custom.orderToView(\''+orderRecord["OrdNumber"]+'\');">';
	            			}else{
	            				htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;color:#4D4D4D" onclick="javascript:custom.orderToView(\''+orderRecord["OrdNumber"]+'\');">';
	            			}
	                			htmlStr +=  '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["OrdNumber"]+'</td>'+
	                						'<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+orderRecord["DetailCount"]+'</td>'+
	                						'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["ShipTo"]+' '+orderRecord["ShipAddress"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+OrdDate2+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+DeliveryDate+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["Payment"]+'</td>'+
										  	'<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">$'+custom.thousandBitSeparator(orderRecord["Amt"],0)+'</td>'+
										  '</tr>'+
										  '<tr style="display:none;" id="'+orderRecord["OrdNumber"]+'_Order_Detail">'+
										  	'<td colspan="7">'+
										  		'<table border="0" cellpadding="0" cellspacing="0" style="width:31.9rem;color:#4D4D4D;">'+
										          '<tr style="height:1.6rem;background-color:#E1E1E1;font-weight:bold;text-align:center;">'+
										            '<td style="border-right:1px solid #CFCFCF;width:4.5rem;">貨品編號</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:12.5rem;">貨品名稱</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:4.2rem;">單位</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:4.2rem;">單價</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">數量</td>'+
										            '<td style="border-right:1px solid #CFCFCF;width:3rem;">項計</td>'+
										          '</tr>';
										          if(orderRecordDetailList && Number(orderRecordDetailList.length)>0){
										          	  for(var k=0;k<orderRecordDetailList.length;k++){
											          	  var orderRecordDetail = orderRecordDetailList.item(k);
											          	  if(orderRecordDetail["OrdNumber"] == orderRecord["OrdNumber"]){
											          	  	  htmlStr +=  '<tr style="height:1.6rem;background-color:#E1E1E1;">'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["item"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["desc2"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;text-align:center;">'+orderRecordDetail["OrdUnit2"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(orderRecordDetail["UnitPrice2"],2)+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+orderRecordDetail["Qty"]+'</td>'+
																            '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">$'+custom.thousandBitSeparator(orderRecordDetail["Amt"],0)+'</td>'+
																          '</tr>';
											          	  }
											          }
										          }
						        htmlStr +=  '</table>'+
							'</td>'+
						  '</tr>';
	            		}
					}
					htmlStr +=   '<tr style="height:2rem;background-color:#FFFFFF;font-weight:bold;border-top:1px solid #CFCFCF;">'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.5rem;text-align:right;" colspan="3">訂單總數</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(qtyTotal,0)+'</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.5rem;text-align:right;" colspan="2">總額</td>'+
								    '<td style="border-right:1px solid #CFCFCF;padding-right:0.2rem;text-align:right;">$'+custom.thousandBitSeparator(totalAmountTotal,0)+'</td>'+
								  '</tr>';
					htmlStr += 	'</table>';
	                Ext.getCmp("order-record-data").setHtml(htmlStr);
					util.hideLoadMask();
				},1500);
			}
		},
		// 客户_客户订单_查看明细
		orderToView : function(OrdNumber) {
			var display = document.getElementById(OrdNumber+"_Order_Detail").style.display;
			for(var i=0;i<config.ord_number_arr.length;i++){
				document.getElementById(config.ord_number_arr[i]+"_Order_Detail").style.display="none";
			}
			if(display == ""){
				document.getElementById(OrdNumber+"_Order_Detail").style.display="none";
			}else{
				document.getElementById(OrdNumber+"_Order_Detail").style.display="";
			}
		},
		// 客户_客户订单_查看明细
		localOrderToView : function(OrderNo) {
			var display = document.getElementById(OrderNo+"_Order_Detail").style.display;
			for(var i=0;i<config.ord_number_arr.length;i++){
				document.getElementById(config.ord_number_arr[i]+"_Order_Detail").style.display="none";
			}
			if(display == ""){
				document.getElementById(OrderNo+"_Order_Detail").style.display="none";
			}else{
				document.getElementById(OrderNo+"_Order_Detail").style.display="";
			}
			config.database.transaction(
		        function (tx) {
		        	tx.executeSql("select * from OrderDetail where SalesCode='"+config.current_user.user_name+"' and OrderNo="+OrderNo+" order by DetailNo", [],
		                function (tx, result) {
		                	var htmlStr = '<table width="100%" border="0" cellpadding="0" cellspacing="0" style="color:#4D4D4D;">'+
									          '<tr style="height:1.6rem;background-color:#E1E1E1;font-weight:bold;text-align:center;">'+
									            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">序號</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">貨品編號</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:6.5rem;">貨品名稱</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">單位</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">數量</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">單價</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:2.5rem;">&nbsp;</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:2.5rem;">項計</td>'+
									            '<td style="border-right:1px solid #CFCFCF;width:11rem;">&nbsp;</td>'+
									          '</tr>';
                    		for(var j=0;j<result.rows.length;j++){
                    			var orderDetailRecord = result.rows.item(j);
                    			htmlStr +='<tr style="height:1.6rem;background-color:#E1E1E1;">'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderDetailRecord['DetailNo']+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderDetailRecord['ItemCode']+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderDetailRecord['ItemName']+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderDetailRecord['Unit']+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+custom.thousandBitSeparator(orderDetailRecord['OrderQty'],0)+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+custom.thousandBitSeparator(orderDetailRecord['Price'],2)+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">$'+custom.thousandBitSeparator(orderDetailRecord['SubTotal'],0)+'</td>'+
								            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
								          '</tr>';
                    		}	  
                    		htmlStr +='</table>';
                    		var trObj=document.getElementById(OrderNo+"_Order_Detail");
							trObj.cells[0].innerHTML = htmlStr;
		                },
		                function (tx, error){
		                	
		                }
		            );
		        }
		    );
		},
		// 删除订单明细
		deleteOrderItemRow : function(index) {
			var orderItemTable=document.getElementById('orderItem');
			var ItemtTrIdNumber = orderItemTable.rows.length;
			if(Number(ItemtTrIdNumber)-1 != index){
				util.showMessageMask('請從最後一行開始依次刪除','warn-max');
			}else{
				navigator.notification.confirm(
					'確定要刪除嗎？',
					function(btnId){
						if(btnId==2){
							var orderItemTable=document.getElementById('orderItem');
							orderItemTable.deleteRow(index);
							config.order_item_arr.pop();
							
							var sumTotal = 0;
							for(var i=0 ; i < orderItemTable.rows.length; i++){
								sumTotal = Number(Number(sumTotal) + Number(orderItemTable.rows[i].cells[5].innerHTML.replace("$","").replace(",","")));
							}
							Ext.getCmp("orderTotal").setHtml("$"+custom.thousandBitSeparator(sumTotal,0));
						}
			        }, '溫馨提示', ['取消','確定']
				);
			}
		},
		// 输入数字
		onkeyupNumber : function(id) {
			document.getElementById(id).value = document.getElementById(id).value.replace(/\D/gi, "");
		},
		// 输入货品编号
		onchangeItemCode : function(id) {
			var ItemCode = document.getElementById(id).value;
			var ItemtTrIdNumber = id.replace("itemId","");
			util.showLoadMask(null);
			config.database.transaction(
		        function (tx) {
		        	tx.executeSql(
		        		"select a.*,c.ItemName as ItemNameChinese,b.ItemShort as Short " +
						" from Item a  " +
						" left join ItemShort b on a.ItemCode = b.ItemCode  " +
						" left join ItemChinese c on a.ItemCode = c.ItemCode  " +
						" where a.ItemCode='"+ItemCode+"' " +
						" and a.SalesCode='"+config.current_user.user_name+"' " +
						" order by a.ItemCode", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				var itme = r.rows.item(0);
		        				var Price = Number(itme["Price"]);
	                			var orderItemTable=document.getElementById('orderItem');
								var tr=orderItemTable.rows[ItemtTrIdNumber];
								if(itme["ItemNameChinese"]){
									tr.cells[1].innerHTML = itme["ItemNameChinese"];
								}else{
									tr.cells[1].innerHTML = itme["ItemName"];
								}
								tr.cells[2].innerHTML = itme["Unit"];
								tr.cells[3].innerHTML = '<input style="border-radius:3px;text-align:center;border:1px solid #888888;height:1.6rem;width:2rem;" onkeyup="javascript:custom.onkeyupOrderNumber(\'itemNum'+ItemtTrIdNumber+'\')" onblur="javascript:custom.onkeyupOrderNumber(\'itemNum'+ItemtTrIdNumber+'\')" type="text" id="itemNum'+ItemtTrIdNumber+'" name="itemNum" value="" />';
								tr.cells[4].innerHTML = "$"+custom.thousandBitSeparator(Price,2);
								tr.cells[5].innerHTML = "$"+custom.thousandBitSeparator(Price,2);
								tr.cells[6].innerHTML = '<img width="16px" src="./resources/images/icon_delete_default.png" onclick="custom.deleteOrderItemRow('+ItemtTrIdNumber+')">';
								var sumTotal = 0;
								for(var i=0 ; i < orderItemTable.rows.length; i++){
									sumTotal = Number(Number(sumTotal) + Number(orderItemTable.rows[i].cells[5].innerHTML.replace("$","").replace(",","")));
								}
								Ext.getCmp("orderTotal").setHtml("$"+custom.thousandBitSeparator(sumTotal,0));
		        			}else{
	                			var orderItemTable=document.getElementById('orderItem');
								var tr=orderItemTable.rows[ItemtTrIdNumber];
								tr.cells[1].innerHTML = ""
								tr.cells[2].innerHTML = "";
								tr.cells[3].innerHTML = '<input style="border-radius:3px;text-align:center;border:1px solid #888888;height:1.6rem;width:2rem;" onkeyup="javascript:custom.onkeyupOrderNumber(\'itemNum'+ItemtTrIdNumber+'\')" onblur="javascript:custom.onkeyupOrderNumber(\'itemNum'+ItemtTrIdNumber+'\')" type="text" id="itemNum'+ItemtTrIdNumber+'" name="itemNum" value="" />';
								tr.cells[4].innerHTML = "";
								tr.cells[5].innerHTML = "";
								tr.cells[6].innerHTML = "";
	                		}
	                		util.hideLoadMask();
		        		},
		        		function (tx, e) {}
		            );
		        }
		    );
		},
		// 输入订单数量
		onkeyupOrderNumber : function(id) {
			document.getElementById(id).value = document.getElementById(id).value.replace(/\D/gi, "");
			var ItemtTrIdNumber = id.replace("itemNum","");
			var orderItemTable=document.getElementById('orderItem');
			var tr=orderItemTable.rows[ItemtTrIdNumber];
			if(tr.cells[4].innerHTML && tr.cells[4].innerHTML!='&nbsp;' && tr.cells[4].innerHTML!='' ){
				tr.cells[5].innerHTML = "$"+custom.thousandBitSeparator(Number(Number(tr.cells[4].innerHTML.replace("$","").replace(",",""))*Number(document.getElementById(id).value)),2);
				var sumTotal = 0;
				for(var i=0 ; i < orderItemTable.rows.length; i++){
					sumTotal = Number(Number(sumTotal) + Number(orderItemTable.rows[i].cells[5].innerHTML.replace("$","").replace(",","")));
				}
				Ext.getCmp("orderTotal").setHtml("$"+custom.thousandBitSeparator(sumTotal,0));
			}
		},
		// 输入订货数
		onkeyupOrderNum : function(id) {
			document.getElementById(id).value = document.getElementById(id).value.replace(/\D/gi, "");
			var itemTotal = Number(document.getElementById(id).value)*Number(Ext.getCmp("unitPrice").getData())
			Ext.getCmp("itemTotal").setHtml(custom.thousandBitSeparator(itemTotal,0))
		},
		// 客户_Call Card记录
		callCardView : function() {
			var endDateStr = Ext.getCmp("callCardEndDate").getComponent('field-value').getData();
			var beginDate = Date.parse(endDateStr);
			beginDate = beginDate-(14*24*60*60*1000);
			beginDate = new Date(beginDate)
			var beginDateStr = '';
			var month = parseInt(beginDate.getMonth()+1);
		    beginDateStr += beginDate.getFullYear();
		    beginDateStr += '-';
			beginDateStr += month < 10 ? '0'+month : month;
			beginDateStr += '-';
			beginDateStr += beginDate.getDate() < 10 ? '0'+beginDate.getDate() : beginDate.getDate();
			util.showLoadMask(null);
			var sql = "";
			for(var i=0 ;i<14; i++){
				var dateDay = Date.parse(beginDateStr);
				dateDay = dateDay+(i*24*60*60*1000);
				dateDay = new Date(dateDay)
				var dateStr = '';
				var curMonth = parseInt(dateDay.getMonth()+1);
			    dateStr += dateDay.getFullYear();
			    dateStr += '';
				dateStr += curMonth < 10 ? '0'+curMonth : curMonth;
				dateStr += '';
				dateStr += dateDay.getDate() < 10 ? '0'+dateDay.getDate() : dateDay.getDate();
				
				sql += ",SUM(CASE WHEN OrdDate='"+dateStr+"' THEN OCTN+OPC ELSE 0 END) AS Qty"+(i+1);
			}
			var beginDateFormatStr = beginDateStr.replace(/\-/g,"");
			var endDateFormatStr = endDateStr.replace(/\-/g,"");
			var productInfo,quantity,qty,callCardRecordDescList;
			config.database.transaction(
		        function (tx) {
		        	tx.executeSql(
		        		"select a.*,c.ItemName as ItemNameChinese,c.PickingSeq as PickingSeq from Item a " +
						" left join ItemChinese c on a.ItemCode = c.ItemCode " +
					  	" where a.SalesCode='"+config.current_user.user_name+"' " +
					  	" and a.ItemCode = '"+config.parameter.ItemCode+"' ", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				productInfo = r.rows.item(0);
		        			}
		        		},
		        		function (tx, e) {}
		            );
		            tx.executeSql(
		        		"select QUANTITY from SalesAllotment " +
						" where DATE >= '"+custom.getCurrentMonthFirst()+"' and DATE <= '"+custom.getCurrentMonthLast()+"' " +
						" and SALESCODE = '"+config.current_user.user_name+"' " +
						" and ITEMCODE = '"+config.parameter.ItemCode+"' ", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				quantity = r.rows.item(0);
		        			}
		        		},
		        		function (tx, e) {}
		            );
		            tx.executeSql(
		        		"select ISNULL(SUM(OCTN+OPC),0) as Qty FROM TAllSalesDataPK " +
						" WHERE " +
						" ORDDATE >= '"+custom.getCurrentMonthFirst()+"' " +
						" and ORDDATE <= '"+custom.getCurrentMonthLast()+"' " +
						" and ITEM='"+config.parameter.ItemCode+"' " +
						" and SRCode = '"+config.current_user.user_name+"' ", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				qty = r.rows.item(0);
		        			}
		        		},
		        		function (tx, e) {}
		            );
		            tx.executeSql(
		        		"select CNTLACCT,OPTFLD4,DESC"+sql+" from TAllSalesDataPK " +
							" where SRCode='"+config.current_user.user_name+"' " +
							" AND CUSTOMER = '"+config.parameter.CustomerCode+"' " +
							" and ORDDATE >= '"+beginDateFormatStr+"' " +
							" and ORDDATE < '"+endDateFormatStr+"' " +
							" group by CNTLACCT,OPTFLD4,DESC", 
		        		[],
		        		function (tx, r) {
		        			if(Number(r.rows.length) > 0){
		        				callCardRecordDescList = r.rows;
		        			}
		        		},
		        		function (tx, e) {}
		            );
		        }
		    );
			setTimeout(function dataShow(){
				var balance = 0;
				if(quantity){
					balance = Number(quantity["QUANTITY"]);
				}
				if(qty){
					balance = balance-Number(qty["QTY"]);
				}
				if(productInfo["ItemNameChinese"]){
    				Ext.getCmp("productName").setHtml(productInfo["ItemNameChinese"]);
    			}else{
    				Ext.getCmp("productName").setHtml(productInfo["ItemName"]);
    			}
    			if(productInfo["Price"]){
    				var Price = Number(productInfo["Price"]);
    				Ext.getCmp("unitPrice").setHtml("$ "+custom.thousandBitSeparator(Price,2));
    				Ext.getCmp("unitPrice").setData(productInfo["Price"]);
    			}
    			Ext.getCmp("itemBalance").setHtml(balance);
    			if(productInfo["Unit"]){
    				Ext.getCmp("unit").setHtml(productInfo["Unit"]);
    			}
    			if(productInfo["PickingSeq"]){
    				Ext.getCmp("packaging").setHtml(productInfo["PickingSeq"]);
    			}
    			if(productInfo["ItemCode"]){
    				Ext.getCmp("itemCode").setHtml(productInfo["ItemCode"]);
    				var image_url = ''+cordova.file.dataDirectory +'/Logo/Product/'+productInfo["ItemCode"]+'.JPG';
    				Ext.getCmp("product-info-image").setHtml('<img style="max-height:10rem;max-width:4.5rem;margin-top: 2rem;margin-bottom: 2rem;" src="'+image_url+'"/>');
    			}
    			var endDateStr = Ext.getCmp("callCardEndDate").getComponent('field-value').getData();
				var beginDate = Date.parse(endDateStr);
				beginDate = beginDate-(14*24*60*60*1000);
				beginDate = new Date(beginDate)
				var beginDateStr = '';
				var month = parseInt(beginDate.getMonth()+1);
			    beginDateStr += beginDate.getFullYear();
			    beginDateStr += '-';
				beginDateStr += month < 10 ? '0'+month : month;
				beginDateStr += '-';
				beginDateStr += beginDate.getDate() < 10 ? '0'+beginDate.getDate() : beginDate.getDate();
				var htmlStr = "";
				Ext.getCmp("call-card-data").setHtml(htmlStr);
				htmlStr += '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-top: 1px solid #CFCFCF;border-bottom: 1px solid #CFCFCF;font-size:0.7rem;color:#4B4B4B;background-color:#EEEEEE;">'+
							  '<tr style="height:1.5rem;text-align:center;border-bottom: 1px solid #CFCFCF;">';
							  	for(var i=0 ;i<14; i++){
									var dateDay = Date.parse(beginDateStr);
									dateDay = dateDay+(i*24*60*60*1000);
									dateDay = new Date(dateDay)
									var dateDayStr = dateDay.getDate();
									htmlStr += '<td style="border-right: 1px solid #CFCFCF;width:7.14%;">'+dateDayStr+'</td>';
								}
							  htmlStr += '</tr>';
				if(callCardRecordDescList && Number(callCardRecordDescList.length)>0){
					var callCardRecordMap = callCardRecordDescList.item(0);
	    			htmlStr += '<tr style="height:1.5rem;text-align:center;border-bottom: 1px solid #CFCFCF;">';
	    			for(var i=0 ;i<14; i++){
	    				if(callCardRecordMap['Qty'+(i+1)]){
	    					htmlStr += '<td style="border-right: 1px solid #CFCFCF;width:7.14%;">'+callCardRecordMap['Qty'+(i+1)]+'</td>';
	    				}else{
	    					htmlStr += '<td style="border-right: 1px solid #CFCFCF;width:7.14%;">&nbsp;</td>';
	    				}
					}
					htmlStr += '</tr>';
				}else{
					htmlStr += '<tr style="height:1.5rem;text-align:center;border-bottom: 1px solid #CFCFCF;">';
	    			for(var i=0 ;i<14; i++){
						htmlStr += '<td style="border-right: 1px solid #CFCFCF;width:7.14%;">&nbsp;</td>';
					}
					htmlStr += '</tr>';
				}
				htmlStr += '</table>';
			   	Ext.getCmp("call-card-data").setHtml(htmlStr);			  
				util.hideLoadMask();
			},1500);
		},
		// 訂單明細
		orderInfoToView : function(OrderNo) {
			Ext.getCmp("orderHome").removeAll();
			Ext.getCmp("orderHome").add({
				xtype : 'orderInfo'
			});
			
			config.database.transaction(
		        function (tx) {
		        	tx.executeSql("select * from OrderHead where SalesCode='"+config.current_user.user_name+"' and OrderNo="+OrderNo, [],
		                function (tx, result) {
		                	if(Number(result.rows.length) > 0){
		                		var orderObj = result.rows.item(0);
		                		Ext.getCmp("OrderNo").setHtml(orderObj['OrderNo']);
	                			Ext.getCmp("CustomerCode").setHtml(orderObj['CustomerCode']);
	                			Ext.getCmp("CustomerName").setHtml(orderObj['CustomerName']);
	                			Ext.getCmp("OrderDateStr").setHtml(orderObj['OrderDate']);
	                			Ext.getCmp("DeliveryDateStr").setHtml(orderObj['DeliveryDate']);
	                			if(Number(orderObj['Payment']) == 1){
	                				Ext.getCmp("PaymentStr").setHtml('月結');
	                			}else if(Number(orderObj['Payment']) == 2){
	                				Ext.getCmp("PaymentStr").setHtml('現款');
	                			}else{
	                				Ext.getCmp("PaymentStr").setHtml('其他');
	                			}
	                			Ext.getCmp("Address").setHtml(orderObj['AddressStr']);
	                			Ext.getCmp("DetailCount").setHtml(orderObj['DetailCount']);
	                			Ext.getCmp("TotalAmount").setHtml("$"+custom.thousandBitSeparator(orderObj['TotalAmount'],0));
	                			Ext.getCmp("addOrder").setData("customer_button_"+orderObj['CustomerCode']+"_"+config.current_user.user_name);
		                	}
		                },
		                function (tx, error){
		                	
		                }
		            );
		            
		            tx.executeSql("select * from OrderDetail where SalesCode='"+config.current_user.user_name+"' and OrderNo="+OrderNo, [],
		                function (tx, result) {
		                	var htmlStr = "";
								Ext.getCmp("order-detail-data").setHtml(htmlStr);
                			if(Number(result.rows.length) > 0){
                				htmlStr += '<table id="orderDetail" width="100%" border="0" cellpadding="0" cellspacing="0" style="background-color:#F5F5F5;border-bottom:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">';
                				for(var i=0;i<result.rows.length;i++){
                					var orderDetailObj = result.rows.item(i);
                					htmlStr += '<tr style="height:1.6rem;">'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:10%;">'+orderDetailObj['DetailNo']+'</td>'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:16%;">'+orderDetailObj['ItemCode']+'</td>'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:30%;">'+orderDetailObj['ItemName']+'</td>'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:10%;">'+orderDetailObj['Unit']+'</td>'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:9%;padding-right:0.2rem;text-align:right;">'+orderDetailObj['OrderQty']+'</td>'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:10%;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(orderDetailObj['Price'],2)+'</td>'+
											    '<td style="padding-left:0.1rem;border-bottom:1px solid #CFCFCF;width:15%;padding-right:0.2rem;text-align:right;">'+custom.thousandBitSeparator(orderDetailObj['SubTotal'],0)+'</td>'+
											  '</tr>';
                					
                				}
                				htmlStr += '</table>';
                				Ext.getCmp("order-detail-data").setHtml(htmlStr);
                			}
		                },
		                function (tx, error){
		                	
		                }
		            );
		        }
		    );
		},
        //缩放图片
        viewImage:function(id,urls){
            var panel;
            var temp = "";
            var newUrls = urls.indexOf(",") == -1 ? urls : urls.substr(0,urls.length-1);
            var urlarr = newUrls.split(",");
            var arr = [];
            for (var i = 0;i<urlarr.length;i++) {
                arr.push({
                    xtype : 'imageviewer',
                    imageSrc : urlarr[i]
                });
            }
            //当类型为图片列表时
            if (urls.indexOf(",") != -1) {
                panel = Ext.Viewport.add({
                    xtype : 'imageCarousel',
                    activeItem : id,
                    items : arr
                });
            } else {
                //类型为文件路径时
                panel = Ext.Viewport.add({
                    xtype : 'imageCarousel',
                    items : [{
                        xtype : 'imageviewer',
                        imageSrc : urls
                    }]
                });
            }
            panel.show();
        }
	}
});